1.在UITableViewCell的数据源方法中,MyTableViewCell是自定义cell,其中有一个UILable,根据lable的高度来确定cell的高度
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifity = @"cellIdentifity";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifity];
if(!cell)
{
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifity];
}
CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(15, 41);
CGRect rect = CGRectInset(cellFrame, 2, 2);
cell.contentLable.frame = rect;
[cell.contentLable sizeToFit];
cell.contentLable.width = self.view.width-30;
cellFrame.size.height = 60 + cell.contentLable.frame.size.height;
[cell setFrame:cellFrame];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = (MyTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}