UITableView didSelectRowAtIndexPath:第一次点击时不会被调用
我遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您是否有可能不小心输入了 didDeselectRowAtIndexPath?
Any chance you accidentally typed didDeselectRowAtIndexPath?
另请检查 xib 文件中表视图的选择属性。根据需要使用“单选”或“多选”。
Also check the selection property of your table view in xib file. Use 'Single Selection' or 'Multiple Selection' as required.
我遇到了以下问题:
就我而言,我的错误是在 Interface Builder 中检查
在触摸时显示选择
。您可以在 IB 中取消选中它:希望对某人有所帮助
I experienced the following issue:
In my case, my error was checking
Show Selection on Touch
in Interface Builder. You can uncheck it in IB here:Hope that helps someone
检查您是否在班级中设置了任何手势识别器。删除手势对我有用。
Check If you have set any Gesture recognisers in your class. Removing gesture worked for me.
我什至对发布这个答案进行了辩论,因为我认为星星是排列整齐的,以便让这一点显现出来。
我遇到了这个问题的变体,并检查了其他解决方案。在我的表格视图中,第一次点击时它不会处理表格的最后一行。它突出显示它,但没有调用
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).为显示该问题的单元格设置了 UITableViewCellSelectionStyleNone
(ios9)。
我最终打电话给
第一件事
,其中不一样,但已经足够好了。
让我想知道ios10会带来什么样的表破损。
UITableViewCellSelectionStyleNone was set for the cell displaying that problem
(ios9).
I have ended up calling
first thing in
which is not the same, but is good enough.
makes me wonder what kind of table breakage ios10 brings.
当您在 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.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 thesuper.viewDidLoad()
SWIFT 3
如果您在不是
UITableViewController
的类中使用 Swift 3,并且您使用的是UITableViewDelegate
,那么您可能需要使用方法标题:这对我有用,尽管我刚刚将我的项目迁移到 Swift 3。它相当新,因此稍后可能会修复。
SWIFT 3
If you are working with Swift 3 in a class which isn't a
UITableViewController
and you are usingUITableViewDelegate
, then you may need to use the method title:This worked for me, although I have just migrated my project to Swift 3. It's fairly new so this may be fixed later.
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
如果您在类中设置了任何 UITapGestureRecognizer,则可以在 didSelectRowAtIndexPath: 中添加此行:
这对我有用。
If you have set any UITapGestureRecognizer in your class, you can add this line in your didSelectRowAtIndexPath:
This worked for me.
Swift 4.2
在属性检查器中禁用“延迟着陆”解决了该问题。
之后,点击变得流畅并且 didSelectRowAt 立即触发。
Swift 4.2
Disabling "Delay Touch Down" in the attributes inspector solved the issue.
After that the clicks are smooth and didSelectRowAt fires immediately.
我的应用程序在第一次或第二次点击时未调用
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就我而言,我有一个带有手势识别器的 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
对我有用的是开始输入“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!!
我在 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 多重选择设置为false
,tableView.allowsMultipleSelection 假
。通过这样做,每次点击单元格时,都会在表格视图上调用
tableView didSelectRowAtIndexPath:
,并且通过选择另一个单元格tableView didDeselectRowAtIndexPath:
将为该单元格调用之前已选择,然后将为新选择的单元格调用tableView didSelectRowAtIndexPath:
。I had the same issue on my tableView(swift 4.2), it could be fixed with
allowsMultipleSelection
property on tableView.If
allowsMultipleSelection
is set totrue
the table view selection mechanism will change in a way that by selecting each cell thetableView didSelectRowAtIndexPath:
is called for the first time and by selecting the same cell for the second time thetableView 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 alwaystableView 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 callingdidSelectRowAtIndexPath:
!!!If you want the
tableView didSelectRowAtIndexPath:
to be called on each selection for a cell then the tableView multiple selection has to be setfalse
,tableView.allowsMultipleSelection = false
.By doing this, every time the cell is tapped
tableView didSelectRowAtIndexPath:
will be called on table view and by selecting another celltableView didDeselectRowAtIndexPath:
will be called for the cell was selected before and then thetableView didSelectRowAtIndexPath:
will be called for the newly selected cell.