我有以下設置:的UITableView:從TableViewController分離數據源和分配#selector
的UITableViewController
子類,稱爲:MainTVC
稱爲一個數據源文件:MainDS
這是一個數據源,以MainTVC
一個單元類稱爲:MainCell
MainCell
有一個名爲的UIButton:
....
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! MainCell
cell.buttonMain.addTarget(MainTVC(), action: #selector(MainTVC.buttonMainTapped(sender:)), for: .touchUpInside)
return cell
}
....
:
buttonMain
buttonMain
將在cellForRowAtIndexPath
在MainDS
(數據源文件)
從MainDS
代碼分配
當buttonMain
被點擊時沒有任何反應,因爲cell.buttonMain.addTarget(MainTVC()
.. < - #selector
需要MainDS使用的**MainTVC**
的實例。
這是我的理解是一個數據源不應該有它的ViewController
的一個實例,因此,它不應該被傳入 - 數據源只是訪問其ViewController
的tableView
。
問題:處理這個問題的最佳做法是什麼?我應該如何分配#selector
位於TableViewController
的cellForRowAtIndexPath
這是我的數據源文件的一部分?
不要調用通過#selector的方法;使用委託或關閉 – Paulw11