大佬教程收集整理的这篇文章主要介绍了ios – 根据自定义单元格增加主tableview行高,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
下面是代码.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"flowviewTableViewCell" owner:self options:nil]; cell2 = [nib objectATindex:0]; return cell2;
“FlowTableViewCell”是一个UITableViewCell.在这个自定义单元格中,我有一个tableview.
我在数组中显示自定义tableview单元格中的一些数据,这些数据的长度各不相同.它不是固定的.
我可以增加自定义单元格大小,但不能增加主tableview行高度,具体取决于自定义tableview单元格的大小.
我想根据自定义tableview单元格的大小动态增加主tableview的单元格大小的高度.
使用以下代码,自定义tableView单元格的高度正在增加.
- (CGFloat)tableView:(UITableView *)tableView heightForRowATindexPath:(NSIndexPath *)indexPath { NSString *str = [arrComments objectATindex:indexPath.row]; CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] consTrainedToSize:CGSizeMake(280,999) lineBreakmode:NSLineBreakByWordWrapping]; NSLog(@"%f",size.height); if (size.height<20) { size.height=20; //m= size.height; } NSLog(@"%f",size.height); return size.height + 30; }
如何根据自定义tableviewcell的大小调整主tableview行高的高度?
以下是我的自定义TableViewCell:
以下是我的主要TableView:
以下是我现在得到的@L_489_26@:
您可以在上面的图像中看到,评论2被剪切,同一篇文章的评论3将在下一篇文章中显示.
我想@L_489_26@如下图像.
所以,我的问题是如何根据自定义tableview单元格的大小动态增加主tableview单元格大小的高度?
请帮助我.非常感谢
首先,在cellForRowATindexPath中创建标签和图像视图:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath { /// Set Text Label UILabel *lbl_myText = [[UILabel alloc]initWithFrame:CGRectZero]; [lbl_myText setLineBreakmode:NSLineBreakByWordWrapping]; lbl_myText.minimumScaleFactor = FONT_SIZE; [lbl_myText setnumberOfLines:0]; lbl_myText.textAlignment = NSTextAlignmentLeft; [lbl_myText setFont:[UIFont systemFontOfSize:FONT_SIZE]]; NSString *text = [arr_text objectATindex:indexPath.row]; CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]]; // checks if text is multi-line if (size.width > lbl_myText.bounds.size.width) { CGSize consTraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2),20000.0f); //// Here Width = Width you want to define for the label in its frame. The height of the label will be adjusted according to this. //CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] consTrainedToSize:consTraint lineBreakmode:NSLineBreakByWordWrapping]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakmode = NSLineBreakByWordWrapping; CGRect textRect = [text boundingRectWithSize:consTraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributename:[UIFont systemFontOfSize:FONT_SIZE],NSParagraphStyleAttributename: paragraphStyle.copy} context:nil]; CGSize size = textRect.size; [lbl_myText setText:text]; [lbl_myText setFrame:CGRectMake(cell.imgv_someoneImage.frame.size.width+8,CELL_CONTENT_MARGIN,CELL_CONTENT_WIDTH - cell.imgv_someoneImage.frame.size.width -(CELL_CONTENT_MARGIN * 2),MAX(size.height,44.0f))]; } else { lbl_myText.frame = CGRectMake(10,cell.frame.size.width - cell.imgv_someoneImage.frame.size.width - 18,18); lbl_myText.textAlignment = NSTextAlignmentLeft; [lbl_myText setText:text]; } //lbl_myText.BACkgroundColor = [UIColor greenColor]; [cell.contentView addSubview:lbl_myText]; /// Set Date Label NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString *StringFromDate = [formatter StringFromDate:[arr_date objectATindex:indexPath.row]]; UILabel *lbl_myDate = [[UILabel alloc]initWithFrame:CGRectMake(cell.imgv_someoneImage.frame.size.width+8,lbl_myText.frame.size.height+10,cell.frame.size.width - cell.imgv_someoneImage.frame.size.width - 10,18)]; lbl_myDate.text = StringFromDate; lbl_myDate.font = [UIFont fontWithName:@"Helvetica Neue" size:13.0]; lbl_myDate.textColor = [UIColor lightGrayColor]; lbl_myDate.textAlignment = NSTextAlignmentLeft; [cell.contentView addSubview:lbl_myDate]; /// Set User Image UIImageView *imgv_myImage = [[UIImageView alloc]initWithFrame:CGRectMake(0,lbl_myText.frame.origin.y,63,63)]; imgv_myImage.image = SELEctedUserUploadedImage; [cell.contentView addSubview:imgv_myImage]; }
这里定义一些常量:
#define FONT_SIZE 15.0f #define CELL_CONTENT_WIDTH 320.0f /// change this according to your screen size. This is just an example #define CELL_CONTENT_MARGIN 10.0f
现在,在创建标签之后,您必须在heightForRowATindexPath中确定单元格的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowATindexPath:(NSIndexPath *)indexPath { NSString *cellText = [arr_text objectATindex:indexPath.row]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString *cellDate = [formatter StringFromDate:[arr_date objectATindex:indexPath.row]]; // NSString *text = [items objectATindex:[indexPath row]]; CGSize consTraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2),20000.0f); //CGSize labelsize = [cellText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] consTrainedToSize:consTraint lineBreakmode:NSLineBreakByWordWrapping]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakmode = NSLineBreakByWordWrapping; ////for message label CGRect textRect = [cellText boundingRectWithSize:consTraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributename:[UIFont systemFontOfSize:FONT_SIZE],NSParagraphStyleAttributename: paragraphStyle.copy} context:nil]; CGSize labelsize = textRect.size; ////for date label CGRect datetextRect = [cellDate boundingRectWithSize:consTraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributename:[UIFont systemFontOfSize:FONT_SIZE],NSParagraphStyleAttributename: paragraphStyle.copy} context:nil]; CGSize datelabelsize = datetextRect.size; //CGSize datelabelsize = [cellDate sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] consTrainedToSize:consTraint lineBreakmode:NSLineBreakByWordWrapping]; ///combine the height CGFloat height = MAX(labelsize.height + datelabelsize.height,64.0f); if(height == 64.0f) { return 74; /// label is of one line,return original/ static height of the cell } else { return height + 10; /// label is of multi-line,return calculated height of the cell + some buffer height } }
以上是大佬教程为你收集整理的ios – 根据自定义单元格增加主tableview行高全部内容,希望文章能够帮你解决ios – 根据自定义单元格增加主tableview行高所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。