TableView裏面的cell根據圖片或者文字自定義高度

1.自定義cell的.h文件裏

/*聲明一個類方法 ,用來計算高度 */

+ (CGFloat)heightWithText: (NSString *)text;


2.自定義.m文件裏面實現

+ (CGFloat)heightWithText: (NSString *)text

{

    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width , 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:17]} context:nil];

    

    return rect.size.height;

   

}



3.方法調用

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

{

    (Cell1爲自定義cell)

   return [Cell1 heightWithText:[self.arr objectAtIndex:indexPath.row]];



}


二.圖片自定義高度

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

{

    UIImage *image = [UIImage imageNamed:[self.arr objectAtIndex:indexPath.row]];

    return 375 * image.size.height / image.size.width;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章