您的位置:首页 > 产品设计 > UI/UE

UITableView的cell 动态 定义 高度

2012-12-19 22:36 239 查看
  首先在
UITableView 的代理方法中算出每个cell 的真实高度,然后设置便ok。如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell;
if (indexPath.section==1) {

UITableViewCell *newsCell = [DataTable dequeueReusableCellWithIdentifier:@"newsCell1"];
if (newsCell==nil) {
newsCell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"newsCell1"]autorelease];
}
CGRect cellFrame = [newsCell frame]; //定义的cell 的 frame
cellFrame.origin = CGPointMake(0,0);

UILabel *productLabel31=(UILabel *)[newsCell.contentView viewWithTag:111143];
if (!productLabel31) {
productLabel31=[[UILabel alloc]initWithFrame:CGRectMake(cellXOffset,6,sectionTwoLabelWidth,10)];
productLabel31.backgroundColor=[UIColor clearColor];
productLabel31.tag=111143;
productLabel31.numberOfLines=0;
productLabel31.lineBreakMode=UILineBreakModeWordWrap;
productLabel31.text=[normalTitleArray objectAtIndex:indexPath.section];
productLabel31.font= [UIFont systemFontOfSize:cellProductLabelFont];

CGSize size = [productLabel31.text sizeWithFont:[UIFont systemFontOfSize:cellProductLabelFont] constrainedToSize:CGSizeMake(sectionTwoLabelWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; //算出cell 的具体高度

productLabel31.frame=CGRectMake(cellXOffset,6,sectionTwoLabelWidth,size.height);
[newsCell.contentView addSubview:productLabel31];
[productLabel31 release];
}
cell = newsCell;
cellFrame.size.height = productLabel31.frame.origin.y+productLabel31.frame.size.height +2; // 改变cell 的frame
[cell setFrame: cellFrame]; // 改变cell 的frame

}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}  最后 还要在UITableView 的代理方法heightForRowAtIndexPath中设置下cell 的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}

 这样cell就可以伴随你cell 内容的多少而 任意改变高度了。 
效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: