[英]Custom UITableViewCell with height defined in UITableView
我有一个自定义的UITableViewCell。 在其函数-(id)initWithStyle:(UITableViewCellStyle)style redirectIdentifier:(NSString *)reuseIdentifier中,我需要了解tableView函数heightForRowAtIndexPath中定义的单元格高度,以便可以在单元格中正确放置UITextField,UIButton等。 。 有任何想法吗?
我通常这样做的方法是在我的NSObject子类中添加一个方法,该方法将充当我的数据源对象(假设您正在使用此基本方法,数据源数组中的内容)。
例如。 假设我们需要显示一堆博客帖子(纯文本),每个帖子都是一个单元格。 由于每一行的高度都会变化,因此我创建了一个NSObject子类,将其称为BlogPostInfo 。 在此类中,我添加方法:
- (int)cellHeight;
{
/* Perform a calculation with blog data, probably using sizeWithFont: */
}
由于数据对象中具有此方法,因此可以在UITableViewController中按以下方式使用它:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
/* assuming blogPosts is an NSMutableArray or whatevs */
return [[blogPosts safeObjectAtIndex:indexPath.row] cellHeight];
}
这就是我做tableviewcells动态高度的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.