[英]Resizing UITableView
我正在做这个项目。 目前,我面临一个问题,我在一个ViewController中有3个UITableView。 看起来像这样:
现在,第一个TableView(顶部的一个)只有一行,大小为55像素,不需要调整大小。 接下来,第二个是TableView,再次具有一行,但是必须根据其内容调整该行的大小。 现在,此TableView显示此行:
该行必须调整大小取决于其文本长度。 我所做的计算大小是:
if(tableView == self.usersPostOnTheWallTableView) {
NSDictionary *propertyItems = [self.repliesItems objectAtIndex:indexPath.row];
NSString *text = [self.postItems objectForKey:@"text"];
NSLog(@"%@", text);
CGSize constraintSize = CGSizeMake(280, MAXFLOAT);
CGSize sizeText = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
self.height = MAX(sizeText.height, 44.0f);
self.photosArray = [propertyItems objectForKey:@"attach_photos"];
self.videosArray = [propertyItems objectForKey:@"attach_videos"];
if((self.photosArray.count == 0) && (self.videosArray.count == 0)) {
height += (CELL_CONTENT_MARGIN * 2);
} else if ((self.videosArray.count > 0) && (self.photosArray.count == 0)) {
height += 90 + (CELL_CONTENT_MARGIN * 2);
} else if((self.photosArray.count > 0) && (self.videosArray.count == 0)) {
height += 85 + (CELL_CONTENT_MARGIN * 2);
}
}
现在,如果文本很小,则很适合该行,并调整了行的大小。 如:
但是,在文本变大之后,会发生以下情况:
显然,我不需要在那里的滚动,因此可以禁用它。 但是这里的事情是,它占用了文本的大小,并调整了标签的大小,您可以看到它从标签的顶部和底部开始有多少空间。 但这并没有在其中放置实际文本。 另一件事是它没有自行调整TableView的大小,当我尝试调整它的大小时,我得到了底表与中间表的重叠。
谁能推荐对此的任何解决方案,或者另一种实现方式?
提前致谢!
您可以使用tableviewDelegate方法根据您的文本设置像元高度,例如:
将为每个单元格调用此方法,以决定应有多少个单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *strDetails = [yourTextArray objectAtIndex:indexPath.row];
//if(indexPath.row == 1)
//{
CGSize stringSize = [strDetails sizeWithFont:[UIFont fontWithName:@"Marker Felt" size:13] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap]; // Provide text as well as font name in which your text will be displayed and constrainedToSize which defines width and height it should be maximum
return stringSize.height+65; //Here 65 can be any value depending on Calculation ImageView height + uppermost space + bottom space added
//}
//else
//{
// return 80 // anything u wishes
//}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.