gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:12 24 4
gpt4 key购买 nike

我遇到了我的单元格背景图像失真的问题,并且在 having it answered 之后然后我开始实现解决方案,该解决方案基本上包括缩短特定违规单元格的高度(自动添加高度)。我这样做如下:

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

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

return standardHeight;
}

但是,每次我运行它时,我都会陷入某种执行循环,应用程序会在该方法的第一行和 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;
}

我做错了什么?我就是想不通。

最佳答案

这里的问题是您不应该依赖一个数据源/委托(delegate)方法来向另一个提供数据。本质上发生的事情是,您询问您的 tableView 它在一个部分中有多少行,您应该何时从您的模型中获取此信息(从哪里 numberOfRowsInSection: 也应该从那里得到它)

您所有的数据源方法都应该直接从模型返回数据,因为您的 tableView 可能会在意想不到的时候向您的数据源请求数据。这不仅适用于 UITableView,而且适用于所有基于数据源的 View ,例如 UICollectionView

关于ios - 为什么在我的应用程序中使用 numberOfRowsInSection 导致永无止境的循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17904024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com