iOS高手进:cell有个按钮,点击按钮展开一行(或插入一行)。如何实现呢?

点击另一个cell的按钮,原来展开的cell缩起,展开新的。
2025-04-13 06:55:30
推荐回答(1个)
回答1:

思路:点击按钮时,改变旧cell、新cell高度,再重新加载指定行。

代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_selectedIndexPath.isEqual(indexPath)) {
        return BIG_HEIGHT; // 展开后高度
    } else {
        return DEFAULT_HEIGHT; // 默认高度
    }
}

- (void)changeButtonClick:(id)sender {
    NSIndexPath *indexPath = [self indexPathForButton:sender]; // 需要自己实现
    NSArray *indexPaths = _selectedIndexPath ? @[indexPath, _selectedIndexPath] : @[indexPath];
    [self.tableView reloadRowsAtIndexPaths:indexPaths];
    _selectedIndexPath = indexPath;
}