重新加载tableview和触摸手势

时间:2011-06-15 07:15:25

标签: iphone objective-c uitableview uitouch

我有一个使用[tableview reloadData];

添加新内容时重新加载的tableview

麻烦的是我在表格中的TableCells上有一个UILongPressGestureRecognizer,因为正在重新加载单元格/表格,LongPress并不总是有时间工作,我猜测它的内部计时器正在被重置/表正在重新加载。

3 个答案:

答案 0 :(得分:4)

您是否尝试在调用UILongPressGestureRecognizer之前查看[tableView reloadData]的状态?例如:

// Returns |YES| if a gesture recognizer began detecting a long tap gesture
- (BOOL)longTapPossible {
    BOOL possible = NO;

    UIGestureRecognizer *gestureRecognizer = nil;
    NSArray *visibleIndexPaths = [tableView indexPathsForVisibleRows];

    for (NSIndexPath *indexPath in visibleIndexPaths) {
        // I suppose you have only one UILongPressGestureRecognizer per cell
        gestureRecognizer = [[tableView cellForRowAtIndexPath:indexPath] gestureRecognizers] 
                                lastObject];
        possible = (gestureRecognizer.state == UIGestureRecognizerStateBegan ||
                    gestureRecognizer.state == UIGestureRecognizerStateChanged);
        if (possible) {
            break;
        }
    }
    return possible;
}

// ... later, where you reload the tableView:

if ([self longTapPossible] == NO) {
    [tableView reloadData];
}

让我知道它是否有效!

答案 1 :(得分:3)

如果您希望保留现有单元格,请不要使用reloadData。相反,当您获得新数据时,请使用Inserting and Deleting Cells的方法向表视图确切地通知哪些单元格已更改。一般程序是:

  1. 您获得了新数据。
  2. 致电beginUpdates
  3. 致电deleteRowsAtIndexPaths:withRowAnimation:以删除新数据中已删除的旧项目的单元格。
  4. 致电insertRowsAtIndexPaths:withRowAnimation:为新数据中添加的任何项目添加新单元格。
  5. 如果您出于某种原因需要有选择地替换特定单元格(并且不能仅使用新数据更新现有单元格的子视图),请使用reloadRowsAtIndexPaths:withRowAnimation:
  6. 致电commitUpdates。此时,您的UITableViewDataSource方法必须反映新数据(例如tableView:numberOfRowsInSection:应反映更改的计数,tableView:cellForRowAtIndexPath:应使用新项目。
  7. 现在,表视图将根据需要调用您的数据源方法来更新显示。现有行不会更改。

答案 2 :(得分:0)

触摸单元格时,将aCellIsSelected等BOOL设置为YES

如果aCellIsSelected为NO,则重新加载tableview

相关问题