Mau*_*hah 4 iphone objective-c uitableview custom-cell ios
我有一个应用程序,其中我有一个Tableview,在该tableview的每一行我动态创建一个自定义的tableview单元格.
下面是代码.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"flowviewTableViewCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
return cell2;
Run Code Online (Sandbox Code Playgroud)
"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;
}
Run Code Online (Sandbox Code Playgroud)
如何根据自定义tableviewcell的大小调整主tableview行高的高度?
在这里,我附上了一些截图,以便清楚地理解.
以下是我的自定义TableViewCell:

以下是我的主要TableView:

以下是我现在得到的输出:

您可以在上面的图像中看到,评论2被剪切,同一篇文章的评论3将在下一篇文章中显示.
我想输出如下图像.

所以,我的问题是如何根据自定义tableview单元格的大小动态增加主tableview单元格大小的高度?
请帮助我.非常感谢
您可以使用以下方法计算标签的高度:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
从TableView委托方法调用此方法:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
return [self heightOfLabel:cell.textLabel];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5155 次 |
| 最近记录: |