2012-12-17 29 views
0

我打電話UITableView代表的heightForRowAtIndexPath內的numberOfRowsInSection方法,但它給了我一個壞訪問錯誤的iOS的UITableView numberOfRowsInSection錯誤訪問

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ... 
    NSLog(@"number of rows in section: %i", [tableView numberOfRowsInSection:[indexPath section]]); 
    ... 
} 

任何人能告訴我什麼是錯在這裏?

+0

你應該向我們展示你'做什麼......因爲這可能是相關的。例如,你實際上是否返回一個值。不這樣做可能會導致BAD ACCESS。 – Besi

+0

你檢查這個問題http://stackoverflow.com/questions/31667986/ios-uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give –

回答

1

這裏需要返回的實際值。因此,不要致電[tableView numberOfRowsInSection:[indexPath section]],而只需再次做同樣的事情。

爲了不重複你的代碼,你可以創建一個輔助方法,你可以在這兩個地方調用,即你的heightForRowAtIndexPath方法和numberOfRowsInSection方法:

- (int) myNumberOfRowsMethod{ 
    // Here you would usually have some underlying model 
    return [self.myContactsOrWhateverArray count]; 
} 
+0

謝謝你,做了這個工作! – user944351

+0

你檢查這個問題http://stackoverflow.com/questions/31667986/ios-uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give –

1

這很正常..在那一刻你的UITableView正在創建。在構建之前,您不應該調用與您的UITableView相關的方法。你應該依靠其他機制來獲得你的單元格的高度。

+0

你檢查這個問題http://stackoverflow.com/questions/31667986/ios -uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give –

2

您正在調用委託方法。

當你調用這個方法,它會調用相關的委託方法像cellForRowAtIndexPathheightForRowAtIndexPath

這將導致無限循環這就是爲什麼它的崩潰。

+0

ca檢查這個問題http://stackoverflow.com/questions/31667986/ios-uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give –

0

要調用

[tableView numberOfRowsInSection:section]; 

你應該叫

[self numberOfRowsInSection:section]; 
+0

再次與downvote和沒有評論!?這有效,比選定的答案要容易得多。好吧。 – Fogmeister

+0

是的。這工作,我不知道downvote是什麼。我正在調用'[self.tableView numberOfRowsInSection:section];'並使用'[self tableView:self.tableView numberOfRowsInSection:section];'解決了這個問題。謝謝:) –

相關問題