【发布时间】:2014-07-21 13:50:41
【问题描述】:
我使用UITableView 来显示数据库中的项目,并且我创建了自定义UITableViewCell,它有一些标签。我最大的标签是名称描述。
每个单元格可以有不同的高度,但我不知道如何设置动态单元格高度。
我得到的只是标签末尾的三个点,单元格的高度始终约为 50。
如何根据其中的标签制作单元格高度?
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = _orderProperty.Description;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]
constrainedToSize:constraint
lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
更新
static NSString *CellIdentifier = @"orderDetailCell";
OrderDetailCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
[tableView registerNib:[UINib nibWithNibName:@"OrderDetailCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
OrderProperty* item = [_list objectAtIndex:indexPath.row];
cell.Title.text = item.Title;
NSString* _description = item.Description;
cell.Description.text = _description;
【问题讨论】:
-
查看此链接用户“2014”回答stackoverflow.com/questions/17358322/…
标签: ios objective-c uitableview ios7