質問

This behavior is iOS 7.1 only, on 7.0, it works as intended.

I've got a UITableView with cells of different height. When tapping on one of them, it expands via

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row != self.selectedCellIndexPath.row) {
        // Close previously opened cell
        if (self.selectedCellIndexPath != nil) {
            [tableView beginUpdates];
            [(MyTableViewCell *) [tableView cellForRowAtIndexPath:self.selectedCellIndexPath] collapse];
            [tableView endUpdates];
        }

        self.selectedCellIndexPath = indexPath;

        [tableView beginUpdates];
        [(MyTableViewCell *) [tableView cellForRowAtIndexPath:indexPath] expand];
        [tableView endUpdates];
    }
    else if (indexPath.row == self.selectedCellIndexPath.row) {
        self.selectedCellIndexPath = nil;
        [tableView beginUpdates];
        [(MyTableViewCell *) [tableView cellForRowAtIndexPath:indexPath] collapse];
        [tableView endUpdates];
    }
}

When tapping on a row at the top of the tableView, everything behaves as intended - the cell is expanded downwards, the top of the cell stays where it is. But when scrolling down and tapping on a cell more at the bottom of the tableview, the cell moves downwards and expands. The further down the cell is, the worse it is.

I'm calculating the table cells heights this way:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.prototypeCell = [[MyTableCell alloc] initReuseIdentifier:CellIdentifier];
    [self configureCell:self.prototypeCell forIndexPath:indexPath];

    float height = 38;
    if ([indexPath isEqual:self.selectedCellIndexPath]) {
        height += [self.prototypeCell expandedHeight] + 5;
    }

    return height;
}

I'm irritated that the effect is different if I tap on a cell at the top or at the bottom of the tableView. Furthermore, this effect is only the first time after scrolling down. When repeating, the cell stays at its position, as intended. Only after scrolling up and down again, it behaves wrong the first time.

役に立ちましたか?

解決

I solved the problem; it was due to estimatedHeightForRowAtIndexPath, I removed the method and now the expanding/collapsing works as expected.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top