ios - didSelectRowAtIndexPath 不起作用,Swift 3

标签 ios swift xcode uitableview

有人能明白为什么 didSelectRowAtIndexPath 不会被调用吗?我在代码和 Storyboard中都由 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

    }
}

}

最佳答案

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

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

注意 _didSelectRowAtdidSelectRowAtIndexPath,就像您更新的其他内容一样(也采用了相同的“样式”),但不是这个。

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

关于ios - didSelectRowAtIndexPath 不起作用,Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40906041/

相关文章:

swift - 核心数据在不同线程中获取和保存

ios - 文件名参数带有通配符的 PathForResource

ios - Xcode 9 : how to install ios 10 sdk

ios - Segue 后页面 View 大小不正确

ios - 从 Firebase 中检索 child 到字典中(Swift 3)

iphone - 两个 View ,在彼此之上,交叉淡入淡出 - iOS

ios - 如何制作自动展开的主页横幅

android - React Native 同步安全随机数生成

ios - 奇怪的 UITableView 错误 : "Assertion Failure" on dequeuing static cells in storyboard

c - Xcode调试窗口如何改变unicode字符串的显示格式?