选择Cell时,UITableViewCell可以更改Cell的大小

时间:2010-07-04 05:12:12

标签: iphone objective-c

我想更改所选单元格的大小。 但是,他们改变了触摸细胞的位置。

-       (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *currentCell = [aTableView cellForRowAtIndexPath:indexPath];

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:currentCell];
[UIView setAnimationDidStopSelector:@selector(endAnimation)];

float extendedSize = currentCell.frame.size.height;
currentCell.frame = CGRectMake(currentCell.frame.origin.x, currentCell.frame.origin.y, currentCell.frame.size.width, currentCell.frame.size.height + extendedSize);

UITableViewCell *afterCell;
for (int i=indexPath.row+1; i<=[aTableView numberOfRowsInSection:indexPath.section]; i++) {
    afterCell = [aTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
    afterCell.frame = CGRectMake(afterCell.frame.origin.x, (afterCell.frame.origin.y + extendedSize), afterCell.frame.size.width, afterCell.frame.size.height);
}
[UIView commitAnimations];
}

谢谢。

1 个答案:

答案 0 :(得分:18)

只是一个简单的例子,但你会明白这个想法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];
    [tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath isEqual:[tableView indexPathForSelectedRow]]) {
        return 88.0;
    }

    return 44.0;
}