Can you animate a height change on a UITableViewCell when selected?

前端 未结 21 1248
天涯浪人
天涯浪人 2020-11-22 04:41

I\'m using a UITableView in my iPhone app, and I have a list of people that belong to a group. I would like it so that when the user clicks on a particular pers

21条回答
  •  执笔经年
    2020-11-22 04:57

    Here is my code of custom UITableView subclass, which expand UITextView at the table cell, without reloading (and lost keyboard focus):

    - (void)textViewDidChange:(UITextView *)textView {
        CGFloat textHeight = [textView sizeThatFits:CGSizeMake(self.width, MAXFLOAT)].height;
        // Check, if text height changed
        if (self.previousTextHeight != textHeight && self.previousTextHeight > 0) {
            [self beginUpdates];
    
            // Calculate difference in height
            CGFloat difference = textHeight - self.previousTextHeight;
    
            // Update currently editing cell's height
            CGRect editingCellFrame = self.editingCell.frame;
            editingCellFrame.size.height += difference;
            self.editingCell.frame = editingCellFrame;
    
            // Update UITableView contentSize
            self.contentSize = CGSizeMake(self.contentSize.width, self.contentSize.height + difference);
    
            // Scroll to bottom if cell is at the end of the table
            if (self.editingNoteInEndOfTable) {
                self.contentOffset = CGPointMake(self.contentOffset.x, self.contentOffset.y + difference);
            } else {
                // Update all next to editing cells
                NSInteger editingCellIndex = [self.visibleCells indexOfObject:self.editingCell];
                for (NSInteger i = editingCellIndex; i < self.visibleCells.count; i++) {
                    UITableViewCell *cell = self.visibleCells[i];
                    CGRect cellFrame = cell.frame;
                    cellFrame.origin.y += difference;
                    cell.frame = cellFrame;
                }
            }
            [self endUpdates];
        }
        self.previousTextHeight = textHeight;
    }
    

提交回复
热议问题