自定义一个cell
//通过model类赋值
-(void)setMModel:(MModel*)mModel
{
//移除重用
for (int i=100; i<_lastLaberCount; i++) {
UILabel *laber=(UILabel *)[self viewWithTag:i];
[laber removeFromSuperview];
}
//通过获取的计数创建Label
for (NSInteger i = 0;i<[mModel.materials count];i++)
{
CGRect rect = [mModel.materials[i] boundingRectWithSize:CGSizeMake(320, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18],NSFontAttributeName,nil] context:nil];
self.Mlabel = [[UILabel alloc] initWithFrame:CGRectMake(0+(i%2)*160, 0+(i/2)*20, 160, rect.size.height)];
self.Mlabel.tag = 100+i;//设置tag值
self.Mlabel.textAlignment = NSTextAlignmentCenter;
self.Mlabel.text = mModel.materials[i];
self.Mlabel.numberOfLines = 0;//自动换行
[self.contentView addSubview:self.Mlabel];
_lastLaberCount=self.Mlabel.tag;
}
}
外部控制器
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MModel *mm = _allkeys[indexPath.row];
CGFloat currentY=0;
for (int i = 0; i< [mm.materials count]; i++) {
CGRect rect = [mm.materials[i] boundingRectWithSize:CGSizeMake(320, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18],NSFontAttributeName,nil] context:nil];
//设置行数自适应
currentY+=rect.size.height-9;
}
return currentY;
}
}