2014-02-12 21 views
1

我想达到以下效果调用heightForRowAtIndexPath:如何从功能

  1. 用户水龙头,如果收缩,或者扩大合约一度对细胞
  2. 细胞膨胀。

到目前为止,我有一个单一的水龙头侦听工作职能:

- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer { 
    CGPoint p = [gestureRecognizer locationInView:self.tableView]; 

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; 
    if(indexPath != nil) { 
     [self.tableView beginUpdates]; 
     *Call heightForRowAtIndexPath here?* 
     [self.tableView endUpdates]; 
    } 
} 

而且我heightForRowAtIndexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"runs"); 
    return 64; 
} 

据我已经能够提供帮助弄清楚从不同的网站在线,我不得不在[self.tableView beginUpdates][self.tableView endUpdates]之间调用函数。 但我该怎么做?我并不熟悉Xcode或Objective C,所以一个好的解释将不胜感激!

在此先感谢。

Aleksander。

回答

4

你自己不叫tableView:heightForRowAtIndexPath:。 tableView会调用它。在将endUpdates发送给它之后,tableView将调用该方法。在调用handleSingleTap:之后,您必须从tableView:heightForRowAtIndexPath:返回不同的值。

您可以通过将展开的indexPath保存在属性(或实例变量)中并在tableView:heightForRowAtIndexPath:中返回不同的值(如果请求的indexPath与保存的展开的indexPath匹配)来完成此操作。

例如

@property (strong, nonatomic) NSIndexPath *expandedIndexPath; 

- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer { 
    CGPoint p = [gestureRecognizer locationInView:self.tableView]; 

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; 
    if(indexPath != nil) { 
     self.expandedIndexPath = indexPath; 
     [self.tableView beginUpdates]; 
     [self.tableView endUpdates]; 
    } 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"runs"); 
    if (self.expandedIndexPath && [indexPath isEqual:self.expandedIndexPath]) { 
     // expanded cell 
     return 200; 
    } 
    else { 
     return 64; 
} 
+0

感谢您的快速反应马提亚! (我不知道这是否是一个问题,但是):当我启动应用程序时,“runs”被打印到控制台4次。我希望这个功能只在一次点击时被调用。此外,还有其他地方我也会使用'beginUpdates'和'endUpdates',例如删除和重新排列单元格。 这会是一个问题吗?这是一个好的解决方案吗?有更好的解决方案吗? 再一次,谢谢:) – Aleksander

+0

'tableView:heightForRowAtIndexPath:'调用tableView中的每个单元格,这就是tableViews的工作方式。这不是问题,而是预期的行为。删除日志。 –

0

如果你打电话给[self.tableView reloadRowsAtIndexPaths:@[yourIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];那么hight for row会被正确调用。
如果你打电话给上面的方法,那么不需要[self.tableView beginUpdate], [self.tableView endUpdate]

+0

让我知道如果这项工作:) –