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