2012-12-06 119 views
2

为什么下面的代码在iOS 6中工作正常?但是在iOS 5中,它陷入了导致设备崩溃的无限循环中。一旦行“self.footerView = ...”被执行,它会再次调用viewForFooterInSection。因此将其陷入无限循环。viewForFooterInSection陷入无限循环

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

    return self.footerView; 
} 

有没有更好的方法来做到这一点?为什么这个工作在iOS 6而不是5?

问候,

彼得

回答

1

这是非常可能的是rectForFooterInSection正在呼叫委托方法tableView:viewForFooterInSection:为了计算视图帧。

对我来说,这似乎是合理的(和预期),这进入了一个无限循环。

你基本上是告诉了footerView的部分xUIViewxfooterView,你应该回报非常相同的同一帧。你能看到递归问题吗?

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

您应该独立定义您的footerView并将其返回到委托方法中。

1

我有这个问题。这种设置页脚的方式最适合我,因为我想定期刷新页脚,并且很好地处理标签。如果你正在递归,只返回nil,苹果将帮助你找出矩形(即使在iOS 4.3到5.1)。

static BOOL alreadyCheckingFooter = false; 
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 
{ 
    if (!self.footerView) { 
     if (alreadyCheckingFooter) 
      return nil; 
     alreadyCheckingFooter = true; 
     self.footerView = [[UIView alloc] initWithFrame:[tableView rectForFooterInSection:section]]; 
     alreadyCheckingFooter = false; 
    } 

    return self.footerView; 
}