根据自定义单元格增加主表格行高度
我有一个应用程序,我有一个TableView,并在该tableview的每一行,我dynamic地创build一个自定义tableview单元格。
下面是这个代码。
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"flowviewTableViewCell" owner:self options:nil]; cell2 = [nib objectAtIndex:0]; return cell2;
“FlowTableViewCell”是一个UITableViewCell。 在这个自定义单元格中,我有一个tableview。
我在数组中显示一些自定义tableview单元格的数据,这些数据长度不一。 这是不固定的。
我能够增加自定义单元格大小,但不是主要的tableview行高度取决于自定义tableview单元格的大小。
我想根据自定义tableview单元的大小dynamic增加主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的大小来调整主桌面视图行高的高度?
在这里,我附上了一些截图,以便清楚理解。
以下是我自定义的TableViewCell:
以下是我的主TableView:
以下是我现在正在获得的输出:
你可以在上面的图片中看到comment2被剪切,同一post的comment3被显示在下一篇文章中。
我想要像下面的图像输出。
所以,我的问题是如何增加dynamic的主桌面视图的单元格大小的高度取决于自定义tableview单元格的大小?
请帮助我。任何帮助将不胜感激
您可以使用下面的代码来dynamic调整高度。 首先,您需要确定标签的高度,然后相应地调整单元格的高度。 我一直在我的聊天应用程序中使用此代码,并正常工作。
首先,在cellForRowAtIndexPath:
创build标签和图像视图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, 0, 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
现在,在创build标签之后,你将不得不在heightForRowAtIndexPath:
确定单元格的高度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 } }
您可以使用以下方法计算标签的高度:
- (CGRect)heightOfLabel:(UILabel*)resizableLable { CGSize constrainedSize = CGSizeMake(resizableLable.frame.size.width , 9999); NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"HelveticaNeue" size:11.0], NSFontAttributeName, nil]; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"textToShow" attributes:attributesDictionary]; CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil ]; if (requiredHeight.size.width > self.resizableLable.frame.size.width) { requiredHeight = CGRectMake(0,0, self.resizableLable.frame.size.width, requiredHeight.size.height); } return requiredHeight; }
从TableView委托方法调用此方法:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; return [self heightOfLabel:cell.textLabel]; }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *str = [arrComments objectAtIndex:indexPath.row]; UIFont *font = [UIFont fontWithName:@"Helvetica" size:14]; CGRect new = [str boundingRectWithSize:CGSizeMake(280, 999) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil]; CGSize size= new.size; NSLog(@"%f",size.height); if (size.height<20) { size.height=20; //m= size.height; } NSLog(@"%f",size.height); return size.height + 30; }
开始了:
- 为视图中的所有单元格创build一个通用的
UITableViewCell
子类(如FlowViewAbstractTableViewCell
) - 在表视图单元格子类上创build一个类方法,如
+ (CGFloat)sizeForCellWithComment:(Comment *)comment
。 你正在做的是设置一个方法来传递一个模型对象到每个表视图单元格子类可以实现。 - 创build您需要从
FlowViewAbstractTableViewCell
inheritance的许多其他单元格子类。 在你的情况,它看起来像你可以有一个名为FlowViewTextCommentTableViewCell
和FlowViewPictureCommentTableViewCell
。 - 对于每个子类,实现
+sizeForCellWithComment:
方法。 这将允许您根据传入方法的对象返回可变大小。 - 实现
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
为您的UITableViewDelegate
。 在这个方法中,你要弄清楚你想要使用哪个单元类,以及哪个对象传入这个大小的方法。 例如:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { Comment *commentForCell = [self.comments objectAtIndex:indexPath.row]; return [CommentTableViewCell sizeForComment:commentForCell].height; }
这将允许您基于对象返回可变单元格高度。 我总是使用可变大小的表格视图单元格,并且喜欢这种模式,因为我认为它隐藏了实现并遵循MVC的最佳工作。
这个video给出了一个关于使用自动布局dynamic调整UITableViewCells大小的教程。 当我需要为一个应用程序做这个事情时,我终于inheritance了UITableViewCell的子类并以编程的方式设置了我的约束条件,而且它的function就像一个魅力一样。
比上述更简单的解决scheme
尝试这个
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.tableName.dataSource tableView:self.tableName cellForRowAtIndexPath:indexPath].contentView.frame.size.height; }
在你的cellForRowAtIndexPath委托中,如果你正在使用标签,那么首先将行数设置为0,然后设置标签文本,然后添加它的sizeTofit属性。 喜欢这个
[cell.yourLabelName setNumberOfLines:0]; cell.yourLabelName.text = @"Your long or short text"; [cell.yourLabelName sizeToFit];
这将按照其中的文本扩展标签高度。
之后,你可以像这样设置你的单元格内容的大小。
cell.contentView.frame = CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width,"Height of label" + cell.contentView.frame.size.height);