#pragma mark--TableView delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str =@"cellid";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
if (!cell) {
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
CGRect cellframe =cell.frame;
if (indexPath.row ==0) {
cell.textLabel.text =self.dataArr[0];
cell.textLabel.numberOfLines =0;
// CGRect rect = [cell.textLabel.text boundingRectWithSize:CGSizeMake(200, 1000) options:NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
cell.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
CGSize maximumLabelSize = CGSizeMake(375, 300);
CGSize rect = [cell.textLabel sizeThatFits:maximumLabelSize];
cellframe.size.height =rect.height +10;
}
else if(indexPath.row ==1)
{
cell.textLabel.text =self.dataArr[1];
// CGRect rect = [cell.textLabel.text boundingRectWithSize:CGSizeMake(200, 200) options:NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
cell.textLabel.numberOfLines =0;
cell.textLabel.lineBreakMode =NSLineBreakByTruncatingTail;
CGSize maximumLabelSize =CGSizeMake(375, 350);
CGSize rect =[cell.textLabel sizeThatFits:maximumLabelSize];
cellframe.size.height =rect.height +25;
}
cell.frame =cellframe;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [selftableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
本文介绍了一个自定义UITableView的实现方式,通过重写tableView:cellForRowAtIndexPath:方法来为不同indexPath.row设置不同高度的UITableViewCell,并调整cell的高度以适应其内容。
5186

被折叠的 条评论
为什么被折叠?



