2012-12-17 32 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];'解决了这个问题。谢谢:) –

相关问题