当我有多个TableView时,如何更改numberOfRowsInSection?

Joh*_*Doe 1 iphone xcode objective-c uitableview ios

这是我目前的代码:

if (tableView == nearbyTV) {
    return numberOfObjects;
}
else {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];
    } 
    else {
        return [users count];
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在尝试根据表视图或表视图搜索显示来反映不同的索引.当用户单击按钮时,将显示第二个视图控制器.此时,如果第二个表视图的索引超过了第一个表视图的索引,它会崩溃,不幸的是熟悉:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 13 beyond bounds [0 .. 12]'
Run Code Online (Sandbox Code Playgroud)

如何更新第二个表的行数?我也是一些如何在滚动时从其他表视图中获取信息.很沮丧.任何帮助都将非常感激.谢谢.

编辑:

的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (tableView == friendsTV || tableView == self.searchDisplayController.searchResultsTableView) {
        static NSString *simpleTableIdentifier = @"SimpleTableCell";

        SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }else{
            cell.propic.image=nil;
            cell.flashBack.backgroundColor=[UIColor whiteColor];
            [cell.button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            cell.fullName.text = [searchResults objectAtIndex:indexPath.row];
        }
        return cell;
        }else{
            static NSString *simpleTableIdentifier = @"nearbyTableCell";

            nearbyTableCell *cell = (nearbyTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

            if (cell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"nearbyTableCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }else{
                cell.propic.image=nil;
                cell.flashBack.backgroundColor=[UIColor whiteColor];
                [cell.button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }
            //    NSLog(@"%@",[searchResults objectAtIndex:indexPath.row]);
            if (tableView == self.searchDisplayController.searchResultsTableView) {
                cell.fullName.text = [searchResults objectAtIndex:indexPath.row];

            }else if (tableView==nearbyTV){
                NSString *requestCombine = [NSString stringWithFormat:@"/%@",[[json objectAtIndex:indexPath.row] objectForKey:@"fbID"]];
                [FBRequestConnection startWithGraphPath:requestCombine
                                             parameters:nil
                                             HTTPMethod:@"GET"
                                      completionHandler:^(
                                                          FBRequestConnection *connection,
                                                          id result,
                                                          NSError *error
                                                          ) {
                                          cell.fullName.text=[result objectForKey:@"name"];

                                      }];
                cell.fbIDLabel.text = [[json objectAtIndex:indexPath.row] objectForKey:@"fbID"];
            }
            return cell;
        }

}
Run Code Online (Sandbox Code Playgroud)

Sal*_*idi 7

问题不在于UITableView在一个类中使用多个s.

从错误日志中:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 13 beyond bounds [0 .. 12]'
Run Code Online (Sandbox Code Playgroud)

很明显,在cellForRowAtIndexPath方法中创建单元格时,您尝试访问索引13处的对象,而数组的长度为13,即最后一个索引为12.因此,尝试调试数组并检查在创建单元格时从数组访问对象的机制.可能是你正在numberOfRowsInSection为你的nearbyTV桌子返还一个额外的计数.

编辑1:Exception Breakpoint在项目中设置它总是一个很好的选择,因为它告诉你崩溃的确切代码位置.设置Exception Breakpoint以跟踪发生崩溃的数组,同时访问元素以跟踪真正的问题.

编辑2:从您的cellForRowAtIndexPath方法中可以看出,searchResults并且json是您从中拾取要在单元格上显示的对象的两个数组.返回的行数numberOfRowsInSection必须searchResultsjson数组和数组中的对象数匹配,以使代码正常工作.问题在于其中一个阵列.调试看到存在于对象的数目searchResultsjson从返回的数组和行计数numberOfRowsInSection方法.