一个ViewController和两个tableview -应用程序崩溃



我有一个单视图控制器管理2表视图。我使用一个标志来跟踪哪个表被选中了。在每个委托函数中,我只是检查标志并使用正确的表。

一切都很好,除了当我加载第二个表时,它的项目比第一个少,当我滚动表时崩溃,因为下面的错误。

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

*第一个抛出调用栈:

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

打印:

Drawing Row = 2 Total num Of items = 0

如果这个表中的项数是正确的,那么为什么这个函数首先被调用?

代码如下:

- (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

编辑-基于您添加的代码:您需要在条件之前定义一个单元格,然后根据条件配置该单元格,然后返回条件之后的单元格。如果你需要一个ImageView和一个TextCell,你可以在条件代码中配置这些对象。

为什么不只是使用一个TableView与两个数据源和切换数据源的需要?

像这样:

@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;  
}

最新更新