为什么在我的应用中使用numberOfRowsInSection导致永无止境的循环?

Dou*_*ith 2 objective-c uitableview ios

我的细胞背景图像被扭曲有问题,在得到答案之后我就去实施解决方案,该解决方案基本上包括缩短特定违规细胞的高度(自动将高度添加到它们中).我这样做如下:

- (CGFloat)tableView:(UITableView *)tableView 
  heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat standardHeight = 44.0;

    if ([tableView numberOfRowsInSection:indexPath.section] == 1) {
        standardHeight -= 2;
    }

    return standardHeight;
}
Run Code Online (Sandbox Code Playgroud)

但是,每次运行时,我都会遇到某种执行循环,应用程序会在该方法的第一行和if语句的开头之间反弹,直到崩溃为止.

视频:http://f.cl.ly/items/2F1E3r2A2p0y1b2j3R14/debug.mov

但是,如果我使用这样的东西(上一个帖子中的答案之一)它似乎工作:

- (CGFloat)tableView:(UITableView *)tableView 
  heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat rowHeight = 44.0f;
    if (indexPath.row == 0) {
        rowHeight -=1;
    }
    return rowHeight;
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我只是想不出来.

Cez*_*zar 6

这里的问题是你不应该依赖一个数据源/委托方法来向另一个提供数据.实际上发生的事情是tableView,当你应该从你的模型中获取这些信息时,你会询问它在一个部分中有多少行(也就是从哪里numberOfRowsInSection:获取它)

您的所有数据源方法都应该直接从模型返回数据,因为您tableView可能会在数据源中询问有关意外时间的数据.这不仅适用UITableView于所有基于数据源的视图,UICollectionView例如.