ios Swift UITableView didSelectRowAtIndexPath 没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29065219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift UITableView didSelectRowAtIndexPath not getting called
提问by Mark
New to IOS development and am having trouble with handling cell selection on a table. Whenever I select, the method is not getting called below - any idea why?
IOS 开发新手,在处理表格上的单元格选择时遇到问题。每当我选择时,该方法都不会在下面被调用 - 知道为什么吗?
My project structure is: View Controller -> View -> Table View
我的项目结构是:View Controller -> View -> Table View
The below code demonstrates the method calls. The others get called no problem! I know touch is working as pull down successfully refreshes and on clicking a cell it does become highlighted.
下面的代码演示了方法调用。其他人被调用没问题!我知道触摸在下拉成功刷新时正在工作,并且在单击单元格时它会突出显示。
import UIKit
class ViewController: UIViewController, UITableViewDelegate
{
let blah = ["blah1"]
//How many sections are in the table?
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//How many rows? (returns and int)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return blah.count
}
//table contents for each cell?
//Each time this is called it'll return the next row and thus build a table...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("Populating each cell of table view!\n")
tableView.rowHeight = 80.0
var cell = UITableViewCell()
var(a) = blah[indexPath.row]
var image : UIImage = UIImage(named: a)!
cell.imageView.image = image
return cell
}
//Code Cell Selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
println("You selected cell #\(indexPath.row)!")
}
func tableView(tableView: UITableViewDelegate, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("wananananaanan" )
println("You deselected cell #\(indexPath.row)!")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
回答by Himani Sharma
Everybody is mentioning to set dataSource and delegate of the tableView. But after setting also not working fine then sometimes it may happen because of none or disable selection of table view.
每个人都提到要设置 tableView 的 dataSource 和委托。但是设置后也不能正常工作,有时可能会因为没有或禁用表视图的选择而发生。
To enable it
Go to storyboard -> Select tableView -> click on the attribute inspector ->go to selector -> Select selection as single selection (or multiple selection according to the requirements.)
启用它转到情节提要 -> 选择 tableView -> 单击属性检查器 -> 转到选择器 -> 选择选择作为单选(或根据要求进行多选。)
Please find attached screenshot for your suitability.
请找到随附的屏幕截图以了解您的适用性。
回答by Victor Sigler
You have to set an @IBOutlet
to the tableView
in you ViewController
and set as it's delegate
and dataSource
to you can see the data an respond to changes in the tableView
.
你必须设置一个@IBOutlet
对tableView
你ViewController
和设置,因为它是delegate
和dataSource
你可以看到数据在变化的响应tableView
。
Something like this :
像这样的事情:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
And implements the UITableViewDataSource
protocol too.
并且也实现了UITableViewDataSource
协议。
Or you can too in the Interface Builder set the ViewController
as it's delegate and dataSource
(more easy to do I think) and avoid to set manually in code like above. Is up to you.
或者您也可以在 Interface Builder 中将其设置ViewController
为委托和dataSource
(我认为更容易做到)并避免在上面的代码中手动设置。你决定。
I hope this help you.
我希望这对你有帮助。
回答by Lineesh K Mohan
SWIFT 3
斯威夫特 3
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Do here
}
Use the above delegate method in swift 3
在 swift 3 中使用上面的委托方法
回答by Hyman
Couple of checks that can help you:-
myTableView.allowsSelection = true
myTableView.delegate = self
Make sure you written
didSelectRowAt
correctly:func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
If you are using
UIButton
onUITableViewCell
then it overlaps cell so check Solution here
回答by Ivan V
I faced the same issue when compared two identical code examples where one was working well and the other was not calling didSelectRowAtIndexPath
在比较两个相同的代码示例时,我遇到了同样的问题,其中一个运行良好,另一个没有调用 didSelectRowAtIndexPath
Take a look at two possible ways to solve the issue:
看一下解决问题的两种可能方法:
1) In the code itself:
1)在代码本身中:
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
table.delegate = self
table.dataSource = self
//data source might be already set if you see contents of the cells
//the main trick is to set delegate
}
2) Using Storyboard or Document Outline (which was the problem in my case cause storyboard changes are not visible in .swift controller classes.
2) 使用 Storyboard 或 Document Outline(在我的案例中这是问题,因为在 .swift 控制器类中看不到故事板更改。
Open Document Outline and Control + Press
your TableView
you will see two outlets called "delegate
" and "dataSource
"
drag them 1 by 1 to the containing ViewController
(right onto the yellow circle)
打开文档大纲,Control + Press
您TableView
将看到两个名为“ delegate
”和“ dataSource
”的出口,将它们一一拖到包含ViewController
(右侧到黄色圆圈上)
That's it!
就是这样!
回答by Victor Ruiz.
You have to use this: First take a look what are you extending and then use the tableView method.
你必须使用这个:首先看看你在扩展什么,然后使用 tableView 方法。
class YourViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var mUITableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// We need to tell to UITableView that we will add the data by ourselves
self.mUITableView.delegate = self
self.mUITableView.dataSource = self
// Register the UITableViewCell class with the tableView
self.mUITableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentifier)
// Setup table data
getEvents()
self.mUITableView.allowsSelection = true
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// here to create you cell view
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "subtitleCell")
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.textLabel?.text = "\(tableData[indexPath.row].name) - (\(tableData[indexPath.row].eventStateId))"
cell.detailTextLabel?.text = tableData[indexPath.row].lastUpdate
return cell
}
}
回答by Ahmed Safadi
Another reason you may write this function which allowed to click under condition
您可以编写此允许在条件下单击的函数的另一个原因
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if(indexPath.section == 1){
return true
}
return false
}
回答by Pixel
Another caveat which took me ages to figure out is to make sure that all three of your Table View, your Cell and your Content View all have User Interaction Enabled. Then in Swift 4, at least, you can use:
我花了很长时间才弄清楚的另一个警告是确保您的表视图、单元格和内容视图中的所有三个都启用了用户交互。然后,至少在 Swift 4 中,您可以使用:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
回答by Tatarasanu Victor
if you are editing your tableView:
如果您正在编辑您的 tableView:
tableView.allowsSelectionDuringEditing = true