Custom UITableView Dynamic Cell Height

前端 未结 6 630
一个人的身影
一个人的身影 2021-02-06 09:22

I have search and searched through endless blogs and articles on how to determine a dynamic height for a custom UITableViewCell and its detailed text. I have really had a hard t

6条回答
  •  我在风中等你
    2021-02-06 10:04

    Hey there so you are going to need to store the list of strings in an NSArray and then you are going to need to calculate the height of the nsstring using sizeWithFont:constrainedToSize: the documentation is found here http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html

    so your tableView method should look something like

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:"Helvetica" size:9] forKey: NSFontAttributeName];
    
         CGSize cell_size = [string boundingRectWithSize:CGSizeMake(300,999) 
                                options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin     
                                attributes:stringAttributes context:nil].size;
    if (cell_size.height > 70)
    {
    return cell_size.height;
    }
    else 
    {
    return 70;
    }
    }
    

    EDIT : THIS has been updated for iOS 7

提交回复
热议问题