开发者

Retracting Table view cell with didSelectRowAtIndexPath

开发者 https://www.devze.com 2023-02-22 19:21 出处:网络
I\'m trying to make a table with a cell which can be clicked, and then grows bigger to show all the info. This works so far with the following code

I'm trying to make a table with a cell which can be clicked, and then grows bigger to show all the info. This works so far with the following code

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Note: Some operations like calling [tableView cellForRowAtIndexPath:indexPath]
    // will call height开发者_开发百科ForRow and thus create a stack overflow
    if(selectedCellIndexPath != nil
       && [selectedCellIndexPath compare:indexPath] == NSOrderedSame){
    labelSize = [[items objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:13.0] 
                         constrainedToSize:CGSizeMake(220.0f, MAXFLOAT) 
                             lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + 40;
    }
    return 68;
}

This works fine and makes the cell bigger. But there is no way to close the cell. I've tried to read, in this case the labelSize.heigth, but this does not correspond to the actual heigth of the clicked cell.

Does anybody knows a good way to close a cell, or in other words. When the cell is tapped the second time there needs to be set the height of 68.

Thanks!


You can check your selectedCellIndexPath instance variable.

Or do something like this if you want multiple selection:

NSMutableSet *_selectedIndexPaths;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 68.0f;

    if ([_selectedIndexPaths containsObject:indexPath]) {
        height *= 2.0f;
    }

    return height;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_selectedIndexPaths containsObject:indexPath]) {
        [_selectedIndexPaths removeObject:indexPath];
    } else {
        [_selectedIndexPaths addObject:indexPath];
    }

    [UIView animateWithDuration:0.35 
                          delay:0.0 
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];                         
                     }
                     completion:NULL];
}


I'd make this a comment on Phil Larson's answer, but multiline code in comments doesn't work very well.

In the past, at least, Apple's recommended using an empty beginUpdates / endUpdates block to get animated cell resizing. This is the first time I've seen UIView animations used to achieve the same effect. I've used the technique in my own code. From Phil's code, I'd replace the [UIView animateWithDuration... with the following

[tableView beginUpdates];
[tableView endUpdate];

If I'm wrong and there's a new recommendation to use UIView animation, I'd be curious to see the discussion of it. Wouldn't have occurred to me to go with that approach.

0

精彩评论

暂无评论...
验证码 换一张
取 消