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

前端 未结 21 1384
天涯浪人
天涯浪人 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];
    }
    

提交回复
热议问题