根据自定义单元格增加主tableview行高 [英] Increase the main tableview row height according to the custom cell
问题描述
我有一个应用程序,其中我有一个 Tableview ,在该tableview的每一行我动态创建一个自定义的tableview单元格。
I am having an app in which I have a Tableview and on that tableview's each row I am dynamically creating a custom tableview cell.
下面是代码。
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"flowviewTableViewCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
return cell2;
FlowTableViewCell是一个UITableViewCell。 在这个自定义单元格中,我有一个tableview。
"FlowTableViewCell" is a UITableViewCell. In this custom cell, I have one tableview.
我在数组中显示自定义tableview单元格中的一些数据,这些数据在长度。这不是固定的。
I am showing some data on my custom tableview cell from an array and those data varies in length. It is not fixed.
我可以根据自定义tableview单元格的大小来增加自定义单元格大小,但不能增加主的tableview行高。
我想根据自定义tableview单元格的大小动态增加主tableview单元格大小的高度。
使用以下代码,自定义tableView单元格的高度正在增加。
With the following code, the height of the custom tableView cell is increasing.
- (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;
}
如何调整main的高度tableview的行高取决于自定义tableviewcell的大小?
How can I adjust the height of main tableview's row height depending on the size of custom tableviewcell?
在这里,我附上一些截图以便清楚理解。
Here,I am attaching some of the screenshots for clear understanding.
以下是我自定义的TableViewCell:
The following is my custom TableViewCell:
以下是我的主要TableView:
The following is my main TableView :
以下是我现在得到的输出:
Following is the output I am getting right now:
你可以在上面的图片中看到评论2被剪切,同一篇文章的评论3会在下一篇文章中显示。
You can see in the above image that comment2 gets cut and comment3 of the same post gets displayed in the next post.
我想输出如下图像。
所以,我的问题是如何根据自定义tableview单元格的大小动态增加主tableview单元格大小的高度?
请帮助我。将不胜感激
推荐答案
您可以使用以下用于动态调整高度的代码。首先,您需要确定标签的高度,然后相应地调整单元格的高度。我一直在聊天应用程序中使用此代码并且工作正常。
You can use the following code to adjust the height dynamically. First you need to determine the height of the label and then adjust the height of the cell accordingly. I have been using this code in my chat application and works fine.
首先,在 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
现在,在创建标签后,你必须在 heightForRowAtIndexPath中确定单元格的高度:
Now, after creating the labels, you will have to determine the height of the cell in 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
}
}
这篇关于根据自定义单元格增加主tableview行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!