how to populate a static UITableView

2019-07-26 16:09发布

问题:

I tried several ways but it seems I'm missing something..

Here is the code I use:

- (void)configureView
{
// Update the user interface for the detail item.
  if (self.detailItem) {
    for (int idx=0; idx<16; idx++) {
        NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:idx];
        [self.tableView cellForRowAtIndexPath:indexPath].detailTextLabel.text = [param objectAtIndex:idx];
    }
    self.detailDescriptionLabel.text = [self.detailItem description];
  }
}

I call this on viewDidLoad. My tableview has 16 static cells, and I'm using standard "Subtitle" type cell, so no customization needed on that front. textLabel.text is filled at design time. Also my tableview is in a tableViewController. I also tried with standard population of tableview but it seems static cells don't agree with that way.

What am I doing wrong?


@jrturton I did some changes to see what's going on:

I added these three lines under for line to see if there's anything there:

        UITableView *tv = self.tableView;
        UITableViewCell *cell = [tv cellForRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
        NSString *label = [[NSString alloc] initWithString:cell.textLabel.text];

First of all tv is assigned correctly, I can see that. But other than that cell and label comes empty. Even tv has no row information it seems..

Could it be because tableview has datasource and delegate assigned to the tableviewcontroller.. I think I heard it shouldn't be like that or something...


@jrturton Here it is:

Hope this helps.

回答1:

Some logging would quicky show you that your index path is not valid and therefore no cell is being returned.

To create an NSIndexPath for use in a UITableView, use this method:

[NSIndexPath indexPathForRow:idx inSection:0];

The index path has to contain a row and section for the table to understand it.

You also need to call this from viewWillAppear: rather than viewDidLoad, the static table is not populated that early on. And you must call [super viewWillAppear:animated] first!