自定义UITableView动态单元格高度 [英] Custom UITableView Dynamic Cell Height

查看:55
本文介绍了自定义UITableView动态单元格高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索并搜索了无数博客和文章,内容涉及如何确定自定义UITableViewCell及其详细文本的动态高度.我真的很难找到关于此的任何好的文档.

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 time finding any good documentation on this.

我需要做的是使单元格根据内部文本生长,但不要低于70高度.

What I need to do is have the cell grow according to the the text inside but never go below a height of 70.

我在StackOverflow上尝试过针对此类问题的几种答案,但没有一个起作用.我的整个应用程序即将完成,但是我真的需要先完成此工作,然后再发布它及其麻烦的内容.

I have tried several of the answers for this type of question here on StackOverflow but none of them worked. My whole app is just about finished but I really need to get this accomplished before I release and its troublesome.

这是我正在尝试的方法,但我只是得到了一小部分彼此重叠的单元格.从我阅读的内容中,我还需要找到该单元格中的自定义单元格或textview的框架,但我不确定如何执行此操作或将它们混合在一起以返回一个高度.

Here is what I am trying but I just get a slop of cells overlapping each other. From what I read I need to find the frame if the custom cell or textview in the cell as well but I am not sure how to do that or mix them all together to return one height.

任何帮助将不胜感激,谢谢!

Any Help would be greatly appreciated Thank you!

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

  CGSize aSize;
aSize = [[(Tweet*)[tweets objectAtIndex:indexPath.row]tweet] sizeWithFont:[UIFont 
systemFontOfSize:14]
            constrainedToSize:CGSizeMake(300.0, 1000.0)
                lineBreakMode:UILineBreakModeTailTruncation];

return  aSize.height;
}

推荐答案

前一段时间我遇到了类似的问题,这对我有很大帮助.

I had a similar issue a while back and this helped me tremendously.

#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *text = [self.items objectAtIndex:indexPath.row];
    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];

    return textSize.height + PADDING * 3;
}

这篇关于自定义UITableView动态单元格高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆