iOS UITableViewCell需要按两次才能调用didSelectRowAtIndexPath

时间:2014-10-14 02:40:10

标签: ios objective-c uitableview

我有一个UITableView,有时需要你触摸它两次以选择一个单元格。

更多细节:

  • 只有在桌子完全滚动或完全向下滚动后才需要进行两次触摸。
  • 只有第二次触摸才会调用didSelectRowAtIndexPath
  • 当桌子以自然的“向上滚动位置”打开时,只需按一下即可选择单元格。
  • 如果你只是滚动一点(不是一直向下/向上),只需轻轻一按即可选择单元格。
  • 如果单元格没有填满整个表格并且不需要滚动,则可以正常工作。
  • 一直走到顶部或底部,你必须触摸两次。

我有一种感觉,第一次触摸实际上是可以选择UITableViewCells或以某种方式激活表格。

我检查过的事情:

  • 我的代码绝对不会在任何地方调用didDeselectRowAtIndexPath
  • 没有UIGestureRecognizers正在使用setCancelsTouchesInView:

桌子上的其他设置:

self.tableView.scrollEnabled = YES;
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.bounces = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

造成这种情况的原因是什么?


更新

奇怪的是,设置self.tableView.bounces = YES;解决了问题。

如果有人有更好的答案,我仍在调查根本原因。显然,我希望桌子不要弹跳,但如果它需要关键功能则不行。

4 个答案:

答案 0 :(得分:8)

可能是你实现了didDeselect而不是didSelect?

答案 1 :(得分:2)

斯威夫特:

一旦细胞被点击,它也会被选中,尝试在点击它时立即取消选择。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
    }

要检查的其他事项还有delaysContentTouchescanCancelContentTouches属性。

答案 2 :(得分:1)

Swift 4

您需要使用beginUpdates()和endUpdates()

来包围更新
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // get current cell
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! YourCustomCell

    // change the value

    // Update the tableView
    tableView.beginUpdates()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    tableView.endUpdates()
}

答案 3 :(得分:0)

也许您应该尝试禁用delaysContentTouches:

tableView.delaysContentTouches = NO;