[英]UItableview cell Dyanmic height with UItableview inside the UItableview cell
我要求将供稿和图像列表放置在tableview中,并且每个供稿注释也应与供稿一起显示。
为此,我在UItableview单元格内使用了内部uitableview。 并且注释表视图高度应该是动态的。如何根据注释表视图(内部表视图)的高度来更新Feed表格视图高度。
我附带的这个示例图片包含10条评论,但是以静态大小,我只能显示一条评论,我试图获取评论tableview内容大小的高度并重新加载feeds tableview,这会导致令人作呕的行为。
谁能给我一个更好的方法。
正如@Wain在评论中提到的,最好的方法可能是在单元格中使用多个节,而不是使用UITableView
。 每个部分可以是一个帖子,第一个单元格是“ post”,随后的单元格是评论。 另外,如果您希望在滚动注释时将其张贴在屏幕顶部,则可以将“ post”作为部分标题。
如果这是不可能的(例如,如果您已经将节用于其他用途),那么您可以做的是在构建主表之前计算“子表”的总高度并将这些值存储为主表的单元格高度。 例如,如果您知道您的第一篇文章有7条评论,总共将占据300pt的高度,而另一篇文章则是50pt,那么您就知道您的单元格需要具有350pt的高度。 如果您确实选择了这条路线,但我仍然认为这不是最佳方法,请确保关闭子表上的滚动。 这将减少发生滚动视图冲突的风险,这将使最终用户感到烦恼和困惑。
最好的办法是动态计算表视图中每个单元格的大小。 我有一个要求,我必须做一个像表视图这样的Facebook,其中内容的高度可能可变。 尤其是文字。 所以我试图在我的单元格contentView中找到所有可变大小内容的所需高度,并相应地设置单元格的高度。 以下是一些代码段:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"Cell";
JHAlertDetailViewCell *cell = (JHAlertDetailViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"JHAlertDetailViewCell" owner:self options:nil];
cell = [nibs objectAtIndex:0];
}
JHAlert *alert = [self.alerts objectAtIndex:indexPath.row];
[self configureCell:cell withItem:alert];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [self heightForBasicCellAtIndexPath:indexPath];
}
- (void)configureCell:(JHAlertDetailViewCell *)cell withItem:(JHAlert *)item{
// In this method, you can actually populate all the contents of your cell and calculate the total //height also
}
- (CGFloat)heightForBasicCellAtIndexPath:(NSIndexPath *)indexPath {
static JHAlertDetailViewCell *sizingCell = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.alertsTV dequeueReusableCellWithIdentifier:@"JHAlertDetailViewCell"];
if (sizingCell==nil) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"JHAlertDetailViewCell" owner:self options:Nil];
sizingCell = [nib objectAtIndex:0];
}
});
[self configureCell:sizingCell withItem:[self.alerts objectAtIndex:indexPath.row]];
return sizingCell.whiteView.bounds.size.height+20; // Here, whiteView is the UIOutlet of the main background view of the cell. Its height as calculated in the configureCell: method is used here to return the required height of the cell.
}
最后,我以这种方式解决了我的问题。
我没有使用多个表格视图,而是使用了多个部分。我将Feed Message / image部分的标题和注释保留为该部分的行。这很棒。
@wain谢谢您的提示。这将对其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.