2012-03-12 49 views
1

我已经添加了单击和双击手势识别器到UITableViewCells。但在滚动桌面几次之后,在滑动桌面的滑动手势结束和滚动动画的开始之间会出现更长的暂停。UITableViewCell的性能问题GestureReognizer

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"tableViewCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; 
    singleTap.numberOfTapsRequired = 1; 
    singleTap.numberOfTouchesRequired = 1; 
    [cell addGestureRecognizer:singleTap]; 

    UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)]; 
    doubleTap.numberOfTapsRequired = 2; 
    doubleTap.numberOfTouchesRequired = 1; 
    [singleTap requireGestureRecognizerToFail:doubleTap]; 
    [cell addGestureRecognizer:doubleTap]; 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     // search results 
    } 
    else { 
     // normal table 
    } 

    return cell; 
} 

SingleTap:和doubleTap:方法

- (void)singleTap:(UITapGestureRecognizer *)tap 
{ 
    if (UIGestureRecognizerStateEnded == tap.state) { 
     UITableViewCell *cell = (UITableViewCell *)tap.view; 
     UITableView *tableView = (UITableView *)cell.superview; 
     NSIndexPath* indexPath = [tableView indexPathForCell:cell]; 
     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
     // do single tap 
    } 
} 

- (void)doubleTap:(UITapGestureRecognizer *)tap 
{ 
    if (UIGestureRecognizerStateEnded == tap.state) { 
     UITableViewCell *cell = (UITableViewCell *)tap.view; 
     UITableView *tableView = (UITableView *)cell.superview; 
     NSIndexPath* indexPath = [tableView indexPathForCell:cell]; 
     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
     // do double tap 
    } 
} 

由于初始滚动是光滑我尝试添加手势识别器进if (cell == nil)条件,但他们在那里从来没有加入到细胞中。

我也是最初有加入的tableView而不是单个细胞的手势,但是这造成对取消按钮的searchDisplayController即自来水无法识别的问题。

我会appriciate任何想法,谢谢。

回答

6

的的cellForRowAtIndexPath法,所以你加了太多手势识别的细胞被调用为同一NSIndexPath多次。因此,表现会受到影响。

我的第一个建议是只添加一个手势识别器到表视图。 (我写了一个类似问题的答案:https://stackoverflow.com/a/4604667/550177

但正如你所说的,它会导致searchDisplayController出现问题。也许你可以通过UIGestureRecognizerDelegate的智能实现来避免它们(当水龙头不在一个单元内时,返回-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;中的NO)。

我的第二个建议是:添加手势识别只有一次:

if ([cell.gestureRecognizers count] == 0) { 
    // add recognizer for single tap + double tap 
} 
+0

谢谢,我只是意识到,如果'(细胞==零)'是不正确使用的iOS5和故事,并用同样的解决方案上来作为你的第二个建议,它工作得很好。但我更喜欢你的第一个想法,我会试试看。 – steharro 2012-03-12 23:11:20