2011-04-06 35 views
0

我正在试图制作一个可以单击的单元格表格,然后逐渐增大以显示所有信息。这工作到如下代码使用didSelectRowAtIndexPath缩回表格视图单元格

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Note: Some operations like calling [tableView cellForRowAtIndexPath:indexPath] 
    // will call heightForRow 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; 
} 

这工作正常,使细胞更大。但是没有办法关闭电池。我试过阅读,在这种情况下,labelSize.heigth,但这不符合单击单元格的实际高度。

有没有人知道关闭单元格的好方法,换句话说,当细胞被第二次敲击时需要设置68的高度。

谢谢!

回答

0

您可以检查您的selectedCellIndexPath实例变量。

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

我会想办法让这对菲尔·拉尔森的答案评论,但评论多代码并不能很好地工作:

或者,如果你想多选做这样的事情。

过去至少,苹果推荐使用空的beginUpdates/endUpdates块来获取动画单元格大小。这是我第一次看到UIView动画用于达到相同的效果。我在我自己的代码中使用了这项技术。从菲尔的代码,我会用替换[UIView animateWithDuration...以下

[tableView beginUpdates]; 
[tableView endUpdate]; 

如果我错了,并有使用的UIView动画的一个新的建议,我会很好奇,想看看它的讨论。我不会想到用这种方法。

相关问题