2016-11-08 95 views
10

任何人都可以看到为什么在世界didSelectRowAtIndexPath不会被调用?我在代码和故事板中都通过delegate进行了三重检查。didSelectRowAtIndexPath不起作用,Swift 3

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 

    } 
} 

} 
+3

你没有更新'didSelectRowAtIndexPath:'签名到Swift 3。从doc:'可选的func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)',注意'''didSelectRowAt'和'didSelectRowAtIndexPath',就像你更新的另一个,但不是这个。删除该行并让XCode执行自动完成。否则,您可以用文档中的替换它。 – Larme

+0

杀了它!谢啦。 – TheValyreanGroup

回答

15

斯威夫特3修改方法的签名(有很多的方法也一样,新 “规则”/风格)

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

通知之_didSelectRowAt vs didSelectRowAtIndexPath,就像你更新的其他人(也适用于相同的“风格”),但不是这个。

删除该行并让XCode执行自动完成。否则,您可以用文档中的替换它。

相关问题