简体   繁体   中英

UITableView row height crash

In my iPhone application I have four types of cells. Every cell has it's own height. I'd like to set the height of the row for each table view cell but it crashes in this method:

-(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;
}

How can I resolve this?

You cannot get the cells using cellForRowAtIndexPath inside the method heightForRowAtIndexPath , because the cells are not there yet. They will be created after their heights are set.

You may set the heights of the cells based on their location using indexPath.row .

You cannot use method cellForRowAtIndexPath , this method is called before cellForRowAtIndexPath so the cells don't exists. You should have a list of Height already calculated when heightForRowAtIndexPath is called. Remember to not load all list in tableview because this method ( heightForRowAtIndexPath ) takes longer to load tableview...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM