2013-03-19 69 views
0

我收到了一個包含很多依賴項的大應用程序。對於這種情況,我實現了一個名爲RootTableViewController的類來處理所有必須在每次需要表視圖控制器時執行的操作。tableView numberOfRowsInSection在iOS4上調用tableView viewForHeaderInSection

現在我發現了一個無限循環,我不知道如何解決它。我在RootTableViewController下面的代碼:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
    int numbersOfRowInSection = [self.tableView numberOfRowsInSection:section]; 

    if (numbersOfRowInSection > 0) 
    { 
     // ... 
    } 
    else 
    { 
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 28.0f)]; 
     return view; 
    } 
} 

這完美的作品在iOS 5和iOS 6,但在iOS4的它會導致死循環,因爲[tableView numberOfRowsInSection]呼籲[tableView viewForHeaderInSection]。我怎樣才能解決這個使用表視圖API?它沒有解決方案,我與[ count]的內部數據陣列工作,因爲我有很多表視圖控制器擴展此RootTableViewController與不同的數據源。

回答

0

如果你想獲得行的數據源的段數,而無需訪問內部數據數組,你可以查詢dataSource委託它,像

int numbersOfRowInSection = [self.tableView.dataSource tableView:self.tableView numberOfRowsInSection:section]; 

(未編譯器檢查)

+0

這將工作。在大多數情況下,self.talbeView.dataSource和self.tableView.delegate將指向自己:) – 2013-03-19 19:08:30

1

這是根本不好的風格。你應該繼承或推廣相關的代理方法,但你應該不要致電UITableView.numberofRowsInSection:

但是,你當然已經實施了tableView:numberOfRowsInSection。將其所有功能移至新方法myNumberOfRowsInSection:在那裏也是這樣。它主要是您當前的numberOfRowsInSection的副本。 然後在你這裏的代碼sniplet調用[self myNumberOfRowsInSection:...];

並在tableView:numberOfRowsInSection:section只是做:

return [self myNumberOfRowsInSection:section]; 

應用相同的模式,你可能想叫自己所有的委託方法。將其所有業務邏輯轉移到您自己的方法中,然後只從委託方法和您自己的代碼中調用您自己的方法。

相關問題