问题
I've been scratching my head over that problem for two days now and I really can't find why such behavior is happening... This is really starting to make me sick!
I have an iOS app which uses storyboards built with interface builder. The opening view is a TabBarVC owning 2 tabs:
Tab 1 : Navigation Controller -> TableView Controller1 -> TableView Controller2
Tab 2 : Collection View Controller -> Navigation Controller -> TableView Controller3 -> TableView Controller2(yes I meant to write that)
In each one of TableView Controllers 1 and 3, I use dynamic prototyped cells (style = right detail, accessory = disclosure indicator). In the first one, I set a reuse identifier, let's say TableView1Cell. In my implementation of the tableview, I use the following code to return a cell :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TableView1Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
SFBottles *bottleAtIndex = [self.dataController getCellarBottleAtIndex:indexPath.row];
[cell.textLabel setText:bottleAtIndex.houseName];
[cell.detailTextLabel setText:bottleAtIndex.productName];
return cell;
}
...and I use the following code to segue to TableView Controller2 :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowBottleDetails"])
{
CellarDetailViewController *detailVC = [segue destinationViewController];
detailVC.bottle = [self.dataController getCellarBottleAtIndex:[self.tableView indexPathForSelectedRow].row];
}
}
All of the above works as expected. I do the exact same thing in TableView Controller3 :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TableView3Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
SFBottles *bottleAtIndex = [self.bottlesArray objectAtIndex:indexPath.row];
[cell.textLabel setText:bottleAtIndex.houseName];
[cell.detailTextLabel setText:bottleAtIndex.productName];
return cell;
}
...and when this view controller gets loaded at runtime, I get the following error :
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableView3Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I was however able to clear this error by adding this line to TableView Controller3's viewDidLoad :
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"TableView3Cell"];
But when I do so, neither the subtitle on the right or the disclosure indicator appear on the displayed cells.
Furthermore, selecting a cell doesn't trigger the segue like it should, even though the code is the same as in TableView Controller1's implementation :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"OverviewListToBottleDetails"])
{
CellarDetailViewController *detailVC = [segue destinationViewController];
detailVC.bottle = [self.bottlesArray objectAtIndex:[self.tableView indexPathForSelectedRow].row];
detailVC.navigationItem.rightBarButtonItem = nil;
}
}
So, why do 2 look-alike TableView Controllers using the same cells and seguing the same way to the same controller don't behave the same way at all?
I know this is a lot of information, ask your questions and I will answer ASAP.
Thanks!
来源:https://stackoverflow.com/questions/14250563/dynamic-prototype-cells-in-uitableviewcontroller-using-storyboards