UITableView didSelectRowAtIndexPath:第一次点击时不会被调用

发布于 2024-08-18 18:40:17 字数 306 浏览 9 评论 0原文

我遇到了 UITableView didSelectRowAtIndexPath 的问题。

我的表已设置为当我选择行时它会初始化一个新的视图控制器并推送它。

我第一次点击表中的任何行时,不会调用该方法。一旦我选择另一行,它就开始正常工作。

我已经通过在 didSelectRowAtIndexPath 上设置断点来验证这一点。当向方法添加 NSLog 时,我看到当我选择最终推送新视图控制器的第二行时,我看到控制台中同时出现两个日志语句。

有什么建议吗?

I'm having an issue with UITableView's didSelectRowAtIndexPath.

My table is setup so that when I select row it initializes a new view controller and pushes it.

The first time I tap any row in the table, the method does not get called. Once I select another row, it begins to work as normal.

I have verified this by setting a breakpoint on didSelectRowAtIndexPath. When adding an NSLog to the method I see that when I select the second row that finally pushes the new view controller, I see two log statements appear in the console at the same time.

Any suggestions?

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

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

发布评论

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

评论(16

雨轻弹 2024-08-25 18:40:17

您是否有可能不小心输入了 didDeselectRowAtIndexPath?

Any chance you accidentally typed didDeselectRowAtIndexPath?

深居我梦 2024-08-25 18:40:17

另请检查 xib 文件中表视图的选择属性。根据需要使用“单选”或“多选”。

Also check the selection property of your table view in xib file. Use 'Single Selection' or 'Multiple Selection' as required.

不顾 2024-08-25 18:40:17

我遇到了以下问题:

  • 第一次点击行 ->没有效果,没有选择,从不
  • 第二次点击并关注 ->正确的选择行为,始终

就我而言,我的错误是在 Interface Builder 中检查在触摸时显示选择。您可以在 IB 中取消选中它:

在此处输入图像描述

希望对某人有所帮助

I experienced the following issue:

  • first tap in row -> no effect, no selection, never
  • second tap and following -> correct selection behavior, always

In my case, my error was checking Show Selection on Touch in Interface Builder. You can uncheck it in IB here:

enter image description here

Hope that helps someone

又爬满兰若 2024-08-25 18:40:17

检查您是否在班级中设置了任何手势识别器。删除手势对我有用。

Check If you have set any Gesture recognisers in your class. Removing gesture worked for me.

小苏打饼 2024-08-25 18:40:17

我什至对发布这个答案进行了辩论,因为我认为星星是排列整齐的,以便让这一点显现出来。

我遇到了这个问题的变体,并检查了其他解决方案。在我的表格视图中,第一次点击时它不会处理表格的最后一行。它突出显示它,但没有调用 didSelectRowAtIndexPath 。所有其他行都工作正常。但是,如果我打开表格视图反弹,那么它似乎可以解决问题(但随后你必须处理表格视图反弹)。

I debated even posting this answer because I think the stars kind of aligned in order for this to manifest itself.

I am having a variation of this problem and have checked the other solutions. On my table view it isn't processing the very last row of my table on the first tap. It highlights it, but didSelectRowAtIndexPath isn't being called. All the other rows work fine. But if I turn the tableview bounce on then it seems to solve the problem (but then you have to deal with a tableview bounce).

草莓味的萝莉 2024-08-25 18:40:17

为显示该问题的单元格设置了 UITableViewCellSelectionStyleNone
(ios9)。

我最终打电话给

[tableView deselectRowAtIndexPath:indexPath animated:NO];

第一件事

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

,其中不一样,但已经足够好了。
让我想知道ios10会带来什么样的表破损。

UITableViewCellSelectionStyleNone was set for the cell displaying that problem
(ios9).

I have ended up calling

[tableView deselectRowAtIndexPath:indexPath animated:NO];

first thing in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

which is not the same, but is good enough.
makes me wonder what kind of table breakage ios10 brings.

安静被遗忘 2024-08-25 18:40:17

当您在 tableView 中使用手势识别器时,也会发生此问题,在这种情况下,您不需要删除它们,只需确保您的手势属性 cancelsTouchesInView = false 这是一个布尔值影响识别手势时是否将触摸传递到视图的值。

this issue happens also when you are working with gesture recogniser within a tableView in this case you don't need to remove them, you need only to make sure that your gesture property cancelsTouchesInView = false this is a boolean value affecting whether touches are delivered to a view when a gesture is recognised.

甜味超标? 2024-08-25 18:40:17

SWIFT 2

确保将其设置为 true:

self.tableView.allowsSelection = true

将其放在 viewDidLoad() 之后的右侧上方并在 super.viewDidLoad() 之前

SWIFT 2

Make sure you have this set to true:

self.tableView.allowsSelection = true

Put this above your right after your viewDidLoad() and before the super.viewDidLoad()

坐在坟头思考人生 2024-08-25 18:40:17

SWIFT 3

如果您在不是 UITableViewController 的类中使用 Swift 3,并且您使用的是 UITableViewDelegate,那么您可能需要使用方法标题:

@objc(tableView:didSelectRowAtIndexPath:) func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){...}

这对我有用,尽管我刚刚将我的项目迁移到 Swift 3。它相当新,因此稍后可能会修复。

SWIFT 3

If you are working with Swift 3 in a class which isn't a UITableViewController and you are using UITableViewDelegate, then you may need to use the method title:

@objc(tableView:didSelectRowAtIndexPath:) func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){...}

This worked for me, although I have just migrated my project to Swift 3. It's fairly new so this may be fixed later.

梦萦几度 2024-08-25 18:40:17

SWIFT 3

其他答案都不适用于我的情况,为我解决的问题是:

tableView.delaysContentTouches = false

SWIFT 3

None of the other answers worked in my case, what fixed it for me was:

tableView.delaysContentTouches = false

别低头,皇冠会掉 2024-08-25 18:40:17

如果您在类中设置了任何 UITapGestureRecognizer,则可以在 didSelectRowAtIndexPath: 中添加此行:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

这对我有用。

If you have set any UITapGestureRecognizer in your class, you can add this line in your didSelectRowAtIndexPath:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

This worked for me.

不回头走下去 2024-08-25 18:40:17

Swift 4.2

在属性检查器中禁用“延迟着陆”解决了该问题。
之后,点击变得流畅并且 didSelectRowAt 立即触发。

XCode 属性检查员

Swift 4.2

Disabling "Delay Touch Down" in the attributes inspector solved the issue.
After that the clicks are smooth and didSelectRowAt fires immediately.

XCode attributes inspector

淑女气质 2024-08-25 18:40:17

我的应用程序在第一次或第二次点击时未调用 didSelectRowAt 函数...
我试图解决这个问题找不到任何解决方案。但突然我意识到,我正在使用 view.animation 颜色更改...动画持续时未调用 Delegate 方法

didSelectRowAt function was not called in my app in first or second tap...
I was trying to solve the problem could not find any solution. But suddenly I was recognise, I was using view.animation color change... Delegate method was not called while animation persist

余生再见 2024-08-25 18:40:17

就我而言,我有一个带有手势识别器的 UITableView 部分标题。当点击标题时,它应该在该部分中插入几行并为插入设置动画(如展开/折叠部分)。
第一次展开并点击时,不会触发 didSelectRow 委托方法。对于进一步的点击和展开/折叠操作,它按预期工作。

根据 @Ayoub Nouri 的回答,我将 cancelsTouchesInView 设置为 false,这解决了问题。
tapGestureRecognizer?.cancelsTouchesInView = false

In my case I had a UITableView section header with a gesture recognizer. When the header is tapped, it should insert few rows into that section and animate the insertion (like expand/collapse section).
First time when expanded and tapped, the didSelectRow delegate method was not fired. For further taps and expand/collapse actions, it was working as expected.

Per answer by @Ayoub Nouri I set cancelsTouchesInView to false, and this resolved the issue.
tapGestureRecognizer?.cancelsTouchesInView = false

墨小沫ゞ 2024-08-25 18:40:17

对我有用的是开始输入“didSelect...”,然后让自动更正填写其余部分。显然我的语法的一些细节很奇怪。正如其他人所说,使用 IDE!

What worked for me was to start typing "didSelect..." and then let autocorrect fill in the rest. Apparently some detail of my syntax was wonky. As others have said, Use the IDE!!

梦明 2024-08-25 18:40:17

我在 tableView(swift 4.2)上遇到了同样的问题,可以通过 tableView 上的 allowsMultipleSelection 属性来修复。

如果 allowsMultipleSelection 设置为 true,表格视图选择机制将发生变化,通过选择每个单元格,为该单元格调用 tableView didSelectRowAtIndexPath:第一次和第二次选择同一单元格时,将调用 tableView didDeselectRowAtIndexPath:

这意味着如果单元格被点击的次数是奇数(1、3、5、...),那么总是会调用 tableView didSelectRowAtIndexPath: 并且如果单元格被点击的次数是偶数(2, 4, 6, ...) 那么总是会调用 tableView didDeselectRowAtIndexPath:

这使得在同一单元格的第三次选择时调用tableView didSelectRowAtIndexPath:函数,因此结果是双击调用didSelectRowAtIndexPath:!!!

如果您希望在每次选择单元格时调用 tableView didSelectRowAtIndexPath:,则必须将 tableView 多重选择设置为 falsetableView.allowsMultipleSelection 假

通过这样做,每次点击单元格时,都会在表格视图上调用 tableView didSelectRowAtIndexPath: ,并且通过选择另一个单元格 tableView didDeselectRowAtIndexPath: 将为该单元格调用之前已选择,然后将为新选择的单元格调用tableView didSelectRowAtIndexPath:

    class TableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.allowsMultipleSelection = false
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("This will be called for each cell tap")
    }

I had the same issue on my tableView(swift 4.2), it could be fixed with allowsMultipleSelection property on tableView.

If allowsMultipleSelection is set to true the table view selection mechanism will change in a way that by selecting each cell the tableView didSelectRowAtIndexPath: is called for the first time and by selecting the same cell for the second time the tableView didDeselectRowAtIndexPath: is called.

It means that if the number of times a cell tapped are odd (1, 3, 5, ...) then always tableView didSelectRowAtIndexPath: will be called and if the number of times a cell tapped are even (2, 4, 6, ...) then always tableView didDeselectRowAtIndexPath: will be called.

This makes tableView didSelectRowAtIndexPath: function to be called on the third selection for the same cell and so the result is double tap for calling didSelectRowAtIndexPath:!!!

If you want the tableView didSelectRowAtIndexPath: to be called on each selection for a cell then the tableView multiple selection has to be set false, tableView.allowsMultipleSelection = false.

By doing this, every time the cell is tapped tableView didSelectRowAtIndexPath: will be called on table view and by selecting another cell tableView didDeselectRowAtIndexPath: will be called for the cell was selected before and then the tableView didSelectRowAtIndexPath: will be called for the newly selected cell.

    class TableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.allowsMultipleSelection = false
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("This will be called for each cell tap")
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文