2012-12-06 118 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; 
}