質問

I have a one View controller managing 2 tableviews. I use a flag to track which table is selected. In each of the delegate functions I just check the flag and use the right table.

Everything works great except that when i load the second table which has lesser items than the first one, crashes when I scroll the table , for the following error.

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2 in section at index 0'

* First throw call stack:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Drawing Row = %d Total num Of items = %d", indexPath.row, [[self.fetchedResultsControllerComments fetchedObjects] count]);

Prints this:

Drawing Row = 2 Total num Of items = 0

If the number of items in this table is correct, then why is this function getting called in the first place?

Here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(currentSelectionTableType1)
    {
        // Draw first kind of cell.
        PlainImageCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"ImageCell"];
        if(cell1 == nil)
            cell1 =[[PlainImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ImageCell"];
        [self configureCell1:cell1 atIndexPath:indexPath];
        return cell1;
    }
    // else Draw the second kind of cell
    PlainTextCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"TextCell"];
    if(cell2 == nil)
        cell2 =[[PlainTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextCell"];
     [self configureCell2:cell2 atIndexPath:indexPath];
     return cell2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(currentSelectionTableType1)
        return [[self.fetchedResultsControllerDataSource1 sections] count];
    return [[self.fetchedResultsControllerDataSource2 sections] count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo;
    if(currentSelectionTableType1)
    {
        sectionInfo = [self.fetchedResultsControllerDataSource1 sections][section];
    }
    else
    {
        sectionInfo = [self.fetchedResultsControllerDatasource2 sections][section];
    }
    return [sectionInfo numberOfObjects];

} Thx

役に立ちましたか?

解決

EDIT - based on the code you added: You need to define one cell before your conditional and then configure that cell based on the conditional and then return the cell after the conditional. If you need both an ImageView and a TextCell, you can configure those objects in the conditional code.

Why not just use one TableView with two datasources and switch out the datasources as needed?

Something like this:

@property(nonatomic, strong) NSArray *tableViewDataSource1;
@property(nonatomic, strong) NSArray * tableViewDataSource2;
@property(nonatomic) BOOL  usingDataSource2;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.usingDataSource2) {
        return [self.tableViewDataSource2 count];
    }

    return [self. tableViewDataSource1 count];    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Create the cell before conditional
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];

    // Conditionally configure the cell
    if (self.usingDataSource2) {
        // Configure Cell using self.tableViewDataSource2 data
    } else {
        // Configure Cell using self.tableViewDataSource1 data
    }

    // Return the configured cell after the conditional
    return cell;  
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top