ios - UITableView not reloading correctly -


i've been having problem lot table view either doesn't load cells first time or displays temporary labels. in both cases, when table loaded second time (by button in force reload or going previous view in app , going table) shows supposed to.

im using [table reloaddata] reload table in viewdidappear: method didn't work in viewdidappear: method , in case put in button action.

i'm using method set label of cell have placed in storyboard:

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath     {     static nsstring *cellidentifier = @"cell";     uitableviewcell *cell = [contactstableview dequeuereusablecellwithidentifier:cellidentifier];      if (cell == nil) {         cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];     }     uilabel *label = (uilabel *)[cell viewwithtag:1001];     label.text = [array3 objectatindex:indexpath.row];      return cell; } 

edit

i declare array below implementation like: nsmutablearray * array3; after in viewdidload method declare rest of array: array3 = [nsmutablearray arraywithobjects: nil]; load elements in array calling function within viewdidload method fill array elements.

edit 2 showing more of how populate array - (void)viewdidload {

[super viewdidload]; array3 = [nsmutablearray arraywithobjects: nil];  [self loadarray];  } - (void) loadarray //populate array in here. // use add object method //  [array3 addobject:object]; { 

  1. good news first: method cellforrowatindexpath correct.

  2. there no need invoke reloaddata in neither -viewdidappear: nor -viewwillappear. remark may not hold true if modify content of array3 while view covered, special case.

it not matter of define method, invoke it. reasonable place populate content of array3 in

required init(coder adecoder: nscoder!) 

or

override func viewdidload() 

pitfalls

additionally, need delegate methods (numberofsectionsintableview, numberofrowsinsection, cellforrowatindexpath) returning consistent values, in accordance array. simple technique use number of entries in array drive numberofrowsinsection.

when use reloaddata?

a typical use of reloaddata shown below:

- (void)loadarray {     //populate array in here.     ...     [self.tableview reloaddata]; // possibly postpone main thread } 

Comments