比较重用标识符崩溃

时间:2012-06-10 20:50:48

标签: ios objective-c uitableview

我需要为我的UITableView中的所有单元格设置一个自定义单元格高度。在这种方法中:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

我试试这个:

if ([[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] == @"imageCell")

因为我在故事板中有3个不同的单元格设置了不同的标识符。但是我的应用只是在EXC_BAD_ACCESS崩溃了。

知道为什么吗?

感谢。

2 个答案:

答案 0 :(得分:2)

您正在比较字符串,因此您应该使用isEqualToString:

if ([[[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] isEqualToString:@"imageCell"])

答案 1 :(得分:0)

  

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

你应该计算你的行的高度,而不是得到你的tableView的单元格。

在我看来,在TableView委托的lifeCycle中,第一步(在分配每个UITableViewCell之前),TableView委托为每一行调用heightForRowAtIndexPath(但此时,UItableViewCell未分配)。在第二步中,TableView调用

  
    

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     创建UitableView Cells。