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

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

    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;
    }
    

提交回复
热议问题