ios didSelectRowAtIndexPath 不起作用,Swift 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40491818/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:44:49  来源:igfitidea点击:

didSelectRowAtIndexPath not working, Swift 3

iosswiftxcodeuitableview

提问by TheValyreanGroup

Can anyone see why in the world didSelectRowAtIndexPath would not be called? I have triple checked by delegateboth in the code and in storyboard.

谁能看出为什么不会调用 didSelectRowAtIndexPath ?我delegate在代码和故事板中都进行了三重检查。

class AddCard: UIViewController,UIPopoverPresentationControllerDelegate, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var cardView: UIView!
@IBOutlet weak var tableView: UITableView!

let tableItems = ["Background Color","Background Image","Font Style","Font Color"]
let cellID = "cell"

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func setBackgroundColor (_ color: UIColor) {
    cardView.backgroundColor = color
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath as IndexPath)

    let row = indexPath.row
    cell.textLabel?.text = tableItems[row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    print(indexPath.row)
    let row = indexPath.row
    switch(row){
    case 0:
        let popoverVC = storyboard?.instantiateViewController(withIdentifier: "colorPickerVC") as! ColorPickerViewController
        popoverVC.modalPresentationStyle = .popover
        popoverVC.preferredContentSize = CGSize(width: 284, height: 446)
        if let popoverController = popoverVC.popoverPresentationController {
            popoverController.sourceView = self.view
            popoverController.sourceRect = CGRect(x: 0, y: 0, width: 85, height: 30)
            popoverController.permittedArrowDirections = .any
            popoverController.delegate = self
            popoverVC.delegate = self
        }
        present(popoverVC, animated: true, completion: nil)
        break
    default: break

    }
}

}

回答by Larme

Swift 3 modified the signature of the method (a lot of methods too, new "rules"/style)

Swift 3 修改了方法的签名(方法也很多,新的“规则”/样式)

Replace:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)with
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

替换:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

Notice the _, the didSelectRowAtvs didSelectRowAtIndexPath, like the other ones you updated (which adapted also the same "style"), but not this one.

注意_didSelectRowAtvs didSelectRowAtIndexPath,就像你更新的其他的一样(也适应了相同的“风格”),但不是这个。

Remove the line and let XCode do the autocompletion. Else, you can just replace it with the one from the doc.

删除该行并让 XCode 进行自动完成。否则,您可以将其替换为文档中的那个。