1

I've been having this problem a lot where the table view either doesn't load the cells the first time or displays temporary labels. In both cases, when the table is loaded a second time (by another button in which I force it to reload or by going to the previous view in the app and going back to the table) everything shows up as it was supposed to.

Im using [table reloadData] to reload the table in the viewDidAppear: method as it didn't work in the viewDidAppear: method and in another case I put it in a button action.

I'm also using this method to set the label of the cell that I have placed in the 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 the array below the implementation like: NSMutableArray * Array3; After that in the ViewDidLoad method I declare the rest of the array: Array3 = [NSMutableArray arrayWithObjects: nil]; Then I load the elements in the array by calling a function within the ViewDidLoad method to fill the array with elements.

EDIT 2 Showing more of how I populate the array - (void)viewDidLoad {

[super viewDidLoad];
Array3 = [NSMutableArray arrayWithObjects: nil];
 [self loadArray];

}
- (void) loadArray
//populate array in here.
// I use the add object method
//  [Array3 addobject:object];
{
7
  • Where do you load/define/fill Array3? Your description suggests to me that the datasource is getting filled in async and isn't ready when you first come into showing the view controller. Commented Jul 10, 2015 at 3:48
  • I'm defining it below my implementation in the .m file. Do you know how I might fix it? Commented Jul 10, 2015 at 4:01
  • No way for me to tell you because you don't show it. :-) How does that array get elements? Commented Jul 10, 2015 at 4:02
  • Please check the sentence Im using [table reloadData] to reload the table in the viewDidAppear: method as it didn't work in the viewDidAppear: method and in another case I put it in a button action., specifically your viewDidAppear comment. Commented Jul 10, 2015 at 4:09
  • "Then I load the elements in the array by calling a function within the ViewDidLoad method to fill the array with elements." That doesn't tell anyone the details of how you load the array (such as whether it is synchronous or not). Show the method that you call inside viewDidLoad. Show your viewDidLoad method so we can see where you call the method that loads the array. Commented Jul 10, 2015 at 4:21

4 Answers 4

1
  1. Good news first: Your method cellForRowAtIndexPath is correct.

  2. There is no need to invoke reloadData in neither -viewDidAppear: nor -viewWillAppear. This remark may not hold true if you modify the content of Array3 while your view is covered, which is a special case.

It is not a matter of where you define your method, but where you invoke it. A reasonable place to populate the content of Array3 is in

required init(coder aDecoder: NSCoder!)

or

override func viewDidLoad()

Pitfalls

Additionally, you need all your delegate methods (numberOfSectionsInTableView, numberOfRowsInSection, cellForRowAtIndexPath) to be returning consistent values, in accordance to your array. A simple technique is to use the number of entries in the array to drive numberOfRowsInSection.

When to use reloadData?

A typical use of reloadData is shown below:

- (void)loadArray {
    //populate array in here.
    ...
    [self.tableview reloadData]; // Possibly postpone to main thread
}
Sign up to request clarification or add additional context in comments.

2 Comments

Could you expand on "required init(coder aDecoder: NSCoder!)?" I've never seen this before (I'm using Obj-C)
Sorry, I've tried to populate my array in the -(void)loadView method, but I cant get it to work. Now, the table doesn't even show up at all when I place the code inside that method rather than the ViewDidLoad. Is there anything I should be doing inside the ViewDidLoad too?
0

Give the cell identifier unique for example you can

NSString *cellIdentifier =[NSString stringWithFormate:@"%d",indexPath.row];

Maybe this will help you...

Comments

0

I think the Array3 is nil When the first load.

1 Comment

Yes it is initially set to nil, but I fill it with objects in the ViewDidLoad method right after I set up the array. I've edited my post to reflect that
0

I believe that in your initial set up for Array3 you must allocate this variable to memory to then be used in a different function, like the tableView:cellForIndexPath:

Therefore instead of Array3 = [NSMutableArray arrayWithObjects: nil]; you would use Array3 = [[NSMutableArray alloc] init];

Since you're leaving it as an empty array on this line of code, you would not use [[NSMutableArray alloc] initWithObjects: nil]; because it is literally the same deal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.