【问题标题】:How do I increase the row height of a selected cell?如何增加选定单元格的行高?
【发布时间】:2016-03-27 06:30:00
【问题描述】:

我有一个 TableViewController,其原型单元格如下所示:

我只想在 TableView 数据加载时默认显示标题和日期标签。此外,当用户选择特定单元格时,我希望该单元格展开,以便用户可以阅读描述标签的内容(可以跨越多行)。

我查看了 StackOverflow 上的多个来源,包括:https://stackoverflow.com/a/3075493/4481169

我浏览过的大多数来源都假设当单元格未被选中时,它将返回到特定高度。但是,在某些情况下,我的标题标签的文本可能占据多行,这意味着每个单元格开始时将具有不同的初始行高。

我怎样才能做到这一点,当用户选择一个特定的单元格时,它会展开以显示所有三个标签,而当取消选择该单元格时,该单元格会恢复到原来的高度?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    实现这一点的最简单方法可能是使用 2 个单元原型:

    1. 未选择:有 2 个 UILabel; // 让我们使用 ID:@"NotSelectedCell"
    2. 已选择:包含所有 3 个 UILabel; //@"SelectedCell"

    还需要存储选中单元格的indexPath,所以:

    @implementation InformationTableViewController {
        NSIndexPath* selectedCellIndexPath;
    }
    

    在你的tableView委托方法tableView:cellForRowAtIndexPath:中,你需要根据indexPath在NotSelectedCellSelectedCell之间切换:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if ([indexPath isEqual:selectedCellIndexPath]) {
            NSString* cellIdentifier = @"SelectedCell";
            InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (cell == nil) {
                cell = [[InformationTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            cell.title = // title
            cell.date  = // date
            cell.description = // description
    
            return cell;
        }
        else {
            NSString* cellIdentifier = @"NotSelectedCell";
            InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (cell == nil) {
                cell = [[InformationTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
                    cell.accessoryType = UITableViewCellAccessoryNone;
            }
            cell.title = // title
            cell.date  = // date
    
            return cell;
        }
    }
    

    此外,每次用户选择一个新单元格并重新加载 tableView 时,您都必须保存一个新的 indexPath:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        selectedCellIndexPath = [indexPath copy];
        [self.tableView reloadData];
    }
    

    为了在重新加载之前恢复tableView的滚动位置,您必须执行以下操作:

    CGFloat tableViewContentHeight = self.tableView.contentSize.height;
    [self.tableView reloadData];
    CGFloat newTableViewContentHeight = self.tableView.contentSize.height;
    self.tableView.contentOffset = CGPointMake(0, newTableViewContentHeight - tableViewContentHeight);
    

    【讨论】:

    • 感谢 Roman,我试过了,它成功了。有什么方法可以动画原型细胞的变化?另外,如何保持选中的单元格突出显示?
    • @ivdekov 1. 您可以使用 [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade] 动画重新加载 tableView; 2. 为了突出显示选定的单元格,您可以为 SelectedCell 原型设置背景颜色。如果它是您问题的解决方案,请考虑接受答案
    • 感谢您的回答。在我的 didSelectRowAtIndexPath 方法中,我创建了一个 IndexPaths 数组并存储了我当前选择的 IndexPath 和我之前选择的 IndexPath。然后我调用 tableView.reloadRowsAtIndexPaths 并传入 IndexPaths 数组。然后,在我的 cellForRowAtIndexPath 方法中,如果 indexPath == 当前选择的索引路径,我会更改单元格的背景颜色。这样做可以吗?
    • 当我选择 TableView 底部的行时,屏幕向上滚动。你知道我怎样才能防止这种情况发生吗?
    • @ivdekov 更新了答案;其实你不能阻止向上滚动,所以你必须向下滚动到上一个位置
    【解决方案2】:

    根据您的要求,我建议您执行此步骤

    • 首先不要对描述进行高度限制,甚至不要 在其中添加任何文本,即保持空白
    • 然后,当您获取描述存储在其他数组中的值时
    • didselectrowatindex 的委托方法之后,您可以填写 该描述的值并重新加载它。

    • 所以它将填充描述中的文本并将高度作为 必填

    【讨论】:

    • 感谢您的回复 Moin。我尝试实施您的解决方案并且它有效,但存在故障。当我单击 TableView 底部的单元格时,整个表格视图会向上滚动一点。
    • 它会随着描述标签的打开而向上滚动,所以它会向上移动
    • 如果您觉得答案有帮助,请接受它,这样即使是像我们这样的其他开发人员也可以从中获得帮助。谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    相关资源
    最近更新 更多