首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >didSelectRowAtIndexPath不工作,Swift 3

didSelectRowAtIndexPath不工作,Swift 3
EN

Stack Overflow用户
提问于 2016-11-09 00:22:58
回答 1查看 22.8K关注 0票数 14

有人能明白为什么didSelectRowAtIndexPath不会被调用吗?我已经通过delegate在代码和故事板中进行了三次检查。

代码语言:javascript
复制
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

    }
}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-09 00:37:21

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

替换:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

注意_didSelectRowAt vs didSelectRowAtIndexPath,就像你更新的其他版本一样(它也采用了相同的“风格”),但不是这个。

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

票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40491818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档