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

前端 未结 21 1023
天涯浪人
天涯浪人 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:53

    I resolved with reloadRowsAtIndexPaths.

    I save in didSelectRowAtIndexPath the indexPath of cell selected and call reloadRowsAtIndexPaths at the end (you can send NSMutableArray for list of element's you want reload).

    In heightForRowAtIndexPath you can check if indexPath is in the list or not of expandIndexPath cell's and send height.

    You can check this basic example: https://github.com/ferminhg/iOS-Examples/tree/master/iOS-UITableView-Cell-Height-Change/celdascambiadetam It's a simple solution.

    i add a sort of code if help you

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 20;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath
    {
        if ([indexPath isEqual:_expandIndexPath])
            return 80;
    
        return 40;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Celda";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        [cell.textLabel setText:@"wopwop"];
    
        return cell;
    }
    
    #pragma mark -
    #pragma mark Tableview Delegate Methods
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSMutableArray *modifiedRows = [NSMutableArray array];
        // Deselect cell
        [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
        _expandIndexPath = indexPath;
        [modifiedRows addObject:indexPath];
    
        // This will animate updating the row sizes
        [tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    0 讨论(0)
  • 2020-11-22 04:53

    Swift 4 and Above

    add below code into you tableview's didselect row delegate method

    tableView.beginUpdates()
    tableView.setNeedsLayout()
    tableView.endUpdates()
    
    0 讨论(0)
  • 2020-11-22 04:54

    Swift version of Simon Lee's answer:

    tableView.beginUpdates()
    tableView.endUpdates()
    

    Keep in mind that you should modify the height properties BEFORE endUpdates().

    0 讨论(0)
  • 2020-11-22 04:56

    just a note for someone like me searching for add "More Details" on custom cell.

    [tableView beginUpdates];
    [tableView endUpdates];
    

    Did a excellent work, but don't forget to "crop" cell view. From Interface Builder select your Cell -> Content View -> from Property Inspector select "Clip subview"

    0 讨论(0)
  • 2020-11-22 04:57

    Try this is for expanding indexwise row:

    @property (nonatomic) NSIndexPath *expandIndexPath;
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
    {
    if ([indexPath isEqual:self.expandedIndexPath])
        return 100;
    
    return 44;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSMutableArray *modifiedRows = [NSMutableArray array];
    if ([indexPath isEqual:self.expandIndexPath]) {
        [modifiedRows addObject:self.expandIndexPath];
        self.expandIndexPath = nil;
    } else {
        if (self.expandedIndexPath)
            [modifiedRows addObject:self.expandIndexPath];
    
        self.expandIndexPath = indexPath;
        [modifiedRows addObject:indexPath];
    }
    
    // This will animate updating the row sizes
    [tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationAutomatic];
    
    // Preserve the deselection animation (if desired)
    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ViewControllerCellReuseIdentifier];
        cell.textLabel.text = [NSString stringWithFormat:@"I'm cell %ld:%ld", (long)indexPath.section, (long)indexPath.row];
    
    return cell;
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题