向左滑动手势无法正常工作。 UITableView的

时间:2012-08-22 20:42:12

标签: ios uitableview uigesturerecognizer

我在UITableView上使用滑动手势。我正试图在向左滑动时对TableViewCell执行操作。我注意到正确的细胞被刷过,但由于某种原因,它似乎激活了一个随机的其他细胞。

我怀疑它与指针有关?我对内存指针不是很了解,所以任何帮助都会很棒。处理滑动的代码是:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
    // Get location of the swipe
    CGPoint location = [gestureRecognizer locationInView:self.requestTableView];

    // Get the corresponding index path within the table view
    NSIndexPath *indexPath = [self.requestTableView indexPathForRowAtPoint:location];

    // Check if index path is valid
    if(indexPath)
    {
        //Get the cell out of the table view
        requestCell *cell = (requestCell *)[self.requestTableView cellForRowAtIndexPath:indexPath];

        NSLog(@"%@", [cell.IDLabel text]);


        cell.sendingImage.hidden = false;
        cell.activityIndicator.hidden = false;
        cell.requestorLabel.hidden = true;
        cell.dueDateLabel.hidden = true;
        cell.IDLabel.hidden = true;
        cell.priorityImage.hidden = true;     
    }
}

2 个答案:

答案 0 :(得分:0)

调用“cellForRowAtIndexPath”很可能不会为您提供屏幕上当前可见的单元格实例。 UITableView基于单元重用(如果使用正确) - 这意味着,每次调用“cellForRowAtIndexPath”时,表视图将尝试查找未使用的单元格,如果没有,则创建一个新单元格。 在您的情况下,这意味着您执行滑动手势的单元格与处理滑动时调用cellForRowAtIndexPath方法时获得的单元格不同。而是返回当前未使用的单元格类型实例,并在该实例上执行更改。 解决此问题的一种方法是,不是直接在手势处理程序中更改单元格的属性,而是更改用于填充单元格的数据模型,然后调用tableView reloadCellAtIndexPath...刷新单元格。

答案 1 :(得分:0)

也许您应该将UITableViewCell子类化并将swipeGesture直接添加到单元格中。 只是一个想法。但是没想过。

相关问题