uitableview - viewForFooterInSection 陷入无限循环

标签 uitableview ios5 crash ios6 infinite

为什么以下代码在 iOS 6 中运行良好。但在 iOS 5 中却陷入无限循环,导致设备崩溃。一旦执行“self.footerView = ...”行,它就会再次调用 viewForFooterInSection。因此将其陷入无限循环。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (!self.footerView)
        self.footerView = [[UIView alloc] initWithFrame:[tableView rectForFooterInSection:section]];

    return self.footerView;
}

有更好的方法吗?为什么这在 iOS 6 上有效,但在 iOS 5 上不起作用?

问候,

彼得

最佳答案

很可能 rectForFooterInSection 正在调用委托(delegate)方法 tableView:viewForFooterInSection: 来计算 View 框架。

对我来说,这进入无限循环似乎是合理的(并且是预期的)。

您基本上是在告诉 x 部分的 footerView 是一个与 footerView 具有相同框架的 UIView > 部分 x ,与您应该返回的完全相同。你能看到这里的递归问题吗?

显然,iOS 6 中存在一些实现更改,可以防止无限循环(可能依赖于页脚框架的某些默认值),但上面的委托(delegate)方法实现肯定是错误的。

您应该独立定义您的 footerView 并在委托(delegate)方法中返回它。

关于uitableview - viewForFooterInSection 陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13754489/

相关文章:

ios - 在 iOS 6 中启用自动布局,同时保持向后兼容 iOS 5

c++ - 对象在另一个对象的变量内消失

swift - 无论我如何重新加载 UITableViewCell,它总是随机闪烁

ios - 为每个新推送添加一个新的 UITableViewCell

ios - Swift UIViewController 添加 UISearchController

ios - 在 UITableView 处于编辑模式时选择行

objective-c - NSURLConnectionDataDelegate 方法未调用

iphone - NSObject 没有存储在 objective-c 的 NSMutableArray 中

android - 使用加速度计应用程序时崩溃

iOS 应用程序不断崩溃