2016-11-08 8 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. Из документа: 'optional func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)', обратите внимание на '_'' theSelectRowAt' vs 'didSelectRowAtIndexPath', как и тот, который вы обновили, но не этот. Удалите строку и дайте XCode выполнить автозаполнение. Кроме того, вы можете просто заменить его на документ из документа. – Larme

+0

Убил его! Спасибо чувак. – TheValyreanGroup

ответ

15

Swift 3 модифицировали подпись метода (много методов тоже новые "правила"/стиль)

Заменить:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) с
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

Обратите внимание на _, didSelectRowAt vs didSelectRowAtIndexPath, как и те, которые вы обновили (которые также адаптировали тот же «стиль»), но не этот.

Удалите линию и позвольте XCode выполнить автозаполнение. Кроме того, вы можете просто заменить его на документ из документа.

Смежные вопросы