UITableView 设置行高度崩溃

在iPhone应用中有四种单元,每个高度都不同。在给每个tableView设置高度的方法崩溃。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@", cell);
    if([cell.reuseIdentifier isEqualToString:PastEventWICellIdentifier]){
        return 56;
    }
    if([cell.reuseIdentifier isEqualToString:PastEventWOICellIdentifier]){
        return 56;
    }
    if([cell.reuseIdentifier isEqualToString:EventWICellIdentifier]){
        return 112;
    }
    if([cell.reuseIdentifier isEqualToString:EventWOICellIdentifier]){
        return 112;
    }
    return 56;
}

不同高度不会崩溃。
崩溃是因为你在heightForRowAtIndexPath 方法中调用 了cellForRowAtIndexPath,导致死循环。。。

建议用其他方式判断高度。

原因出在heightForRowAtIndexPath 方法中你使用

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

来获取uitableviewcell会导致无限循环.而致应用崩溃.
有两种方法可以解决:
1.

self.tableView.delegate=nil;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
self.tableView.delegate=self;

2.

UITableViewCell *cell=[self tableView:tableView cellForRowAtIndexPath:indexPath];