【问题标题】:UItableview cell Dyanmic height with UItableview inside the UItableview cellUItableview 单元格的动态高度与 UItableview 单元格内的 UItableview
【发布时间】:2015-01-12 13:00:51
【问题描述】:

我要求在 tableview 中放置提要和图像列表,并且每个提要 cmet 也应该与提要一起显示。

为此,我在 UItableview 单元格中使用了 Inner uitableview。并且 cmets tableview 高度应该是动态的。如何根据 Comments tableview (Inner tableview) 的高度更新 Feeds tableview 高度。

我附上的这张示例图片包含 10 个 cmets,但使用静态尺寸我只能显示一条评论,我试图获取 cmets tableview 内容大小高度并重新加载提要 tableview,这会导致令人作呕的行为。

谁能给我一个更好的方法。

【问题讨论】:

  • 为什么不使用多个部分?
  • «您不应该在 UIScrollView 对象中嵌入 UIWebView 或 UITableView 对象。如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混淆和错误处理。» source。由于 UITableView 是 UIScrollView 的子视图,因此这里适用此规则。

标签: ios iphone uitableview


【解决方案1】:

正如 cmets 中提到的@Wain,最好的方法可能是在单元格中使用多个部分而不是UITableView。每个部分都可以是一个帖子,第一个单元格是“帖子”,后续单元格是 cmets。或者,如果您希望在滚动 cmets 时将“帖子”粘贴到屏幕顶部,则可以将“帖子”设置为部分标题。

如果这是不可能的(例如,如果您已经将部分用于其他目的),您可以做的是在构建主表之前计算“子表”的总高度并将这些值存储到用作主表的单元格高度。例如,如果您知道您的第一个帖子有 7 个 cmets,总共占用 300pt 的高度,以及一个 50pt 的帖子,您就知道您的单元格需要有 350pt 的高度。如果你确实走这条路,我仍然认为这不是最好的方法,请确保关闭子表上的滚动。这将降低滚动视图冲突的风险,从而使最终用户感到烦恼和困惑。

【讨论】:

    【解决方案2】:

    最好的办法是动态计算表格视图的每个单元格的大小。我有一个要求,我必须做一个类似于 facebook 的表格视图,其中的内容可能是可变高度的。尤其是文字。所以我试图在我的单元格 contentView 中找到所有可变大小内容的所需高度,并相应地设置单元格的高度。下面是一些sn-ps的代码:

    -(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.
    }
    

    【讨论】:

      【解决方案3】:

      我终于以这种方式解决了我的问题。

      我选择了多个部分,而不是多个表格视图。我将提要消息/图像部分标题和 cmets 作为该部分的行。这项工作很棒。

      @wain 感谢您的提示。这将对其他人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-04
        相关资源
        最近更新 更多