重新加载桌面视图和触摸手势

发布于 2024-11-16 00:34:40 字数 196 浏览 2 评论 0原文

我有一个在添加新内容时正在重新加载的表格视图,使用 [tableview reloadData];

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

I have a tableview which is being reloaded as new content is added, using [tableview reloadData];

Trouble is I have a UILongPressGestureRecognizer on the TableCells in the Table and because the cells / table are being reloaded quite often the LongPress doesnt always have time to work as, I'm guessing it's internal timers are being reset when the cell/table is being reloaded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2024-11-23 00:34:40

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

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

让我知道它是否有效!

Have you tried looking at the state of your UILongPressGestureRecognizers before [tableView reloadData] is called? For example:

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

Let me know if it works!

想你只要分分秒秒 2024-11-23 00:34:40

如果您希望保留现有单元格,请不要使用reloadData。相反,当您获取新数据时,请使用 插入和删除单元格以准确通知表格视图哪些单元格已更改。一般过程是:

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

Don't use reloadData if you want existing cells to remain. Instead, when you get new data, use the methods for Inserting and Deleting Cells to inform the table view exactly which cells have changed. The general procedure is:

  1. You get new data.
  2. Call beginUpdates
  3. Call deleteRowsAtIndexPaths:withRowAnimation: to remove cells for any old items that have been deleted in the new data.
  4. Call insertRowsAtIndexPaths:withRowAnimation: to add new cells for any items that have been added in the new data.
  5. If you need to selectively replace a particular cell for some reason (and can't just update the existing cell's subviews with the new data), use reloadRowsAtIndexPaths:withRowAnimation:.
  6. Call commitUpdates. At this point, your UITableViewDataSource methods must reflect the new data (e.g. tableView:numberOfRowsInSection: should reflect the changed count, and tableView:cellForRowAtIndexPath: should use the new items).
  7. The table view will now call your data source methods as needed to update the display. Existing rows will not be changed.
暮年 2024-11-23 00:34:40

当您触摸单元格时,将 aCellIsSelected 之类的 BOOL 设置为 YES

,如果 aCellIsSelected 为 NO,则重新加载表格视图

Set a BOOL like aCellIsSelected to YES when you touch the cell

and just reload tableview if aCellIsSelected is NO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文