ios - cellForRowAtIndex is taking time to load cell from xib -
i have created custom uitableview class xib.
in -cellforrowatindex
following code written.
the issue takes 2-3 sec push screen contains tableview
.
(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *simpletableidentifier = @"mycell"; customcell *cell = (customcell *)[tableview dequeuereusablecellwithidentifier:simpletableidentifier]; if (cell == nil) { nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"customcell" owner:self options:nil]; cell = [nib objectatindex:0]; } cell.selectionstyle = uitableviewcellselectionstylenone; if(!indexpath.row) { uiview *separatorview = [[uiview alloc]initwithframe:cgrectmake(20,14, screenwidth-40, 1)]; separatorview.backgroundcolor = [uicolor colorwithwhite:0.4 alpha:0.6]; [cell addsubview:separatorview]; self.view.backgroundcolor = [uicolor whitecolor]; } else { uiview *separatorview = [[uiview alloc]initwithframe:cgrectmake(20,6, screenwidth-40, 1)]; separatorview.backgroundcolor = [uicolor colorwithwhite:0.4 alpha:0.6]; [cell addsubview:separatorview]; self.view.backgroundcolor = [uicolor whitecolor]; } tableview.scrollenabled = yes; self.view.backgroundcolor = [uicolor whitecolor]; return cell; }
i have debug code when return numberofrows
0 screen pushed immediately. if return 1 row takes 2-3 sec load.
i creating tableview
programmatically.
edit
i have added in viewdidload
after creating table:
uinib *cellnib = [uinib nibwithnibname:@"customcell" bundle:nil]; [mtableview registernib:cellnib forcellreuseidentifier:@"mycell"];
and cellforrowatindex method:
customcell *cell = (customcell *)[tableview dequeuereusablecellwithidentifier:@"customcell"]; cell.selectionstyle = uitableviewcellselectionstylenone; if(cell == nil){ cell = (customcell *)[[[nsbundle mainbundle] loadnibnamed:@"customcell" owner:nil options:nil] lastobject]; [cell setrestorationidentifier:@"mycell"]; self.view.backgroundcolor = [uicolor whitecolor]; }
but nothing chaneged
you need register loaded nib once reusing identifier table view can deque identifier, dequeing identifier alltime returning nil guess.
the best place register nib in viewdidload
using
[self.tableview registernib:cellloadedfromnib forcellreuseidentifier:@"mycell"];
then access in cellforrowatindexpath:
using following
[tableview dequeuereusablecellwithidentifier:@"mycell" forindexpath:indexpath];
make sure registering nib in tableview after tableview been initiated.
Comments
Post a Comment