[英]Specifying Segue for UITableViewCell
在我制定布局規划時,我正在將一系列項目暫時放入菜單中,但我無法弄清楚如何使用indexPath.row來處理DidSelectRow -
我有以下代碼:
import UIKit
class DispatchViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var videos: [DispatchMenuItem] = []
override func viewDidLoad() {
super.viewDidLoad()
videos = createArray()
self.tableView.rowHeight = 65.0
tableView.dataSource = self
}
func createArray() -> [DispatchMenuItem] {
let video1 = DispatchMenuItem(image: #imageLiteral(resourceName: "truckUnloadIcon"), title: "Open Manifests")
let video2 = DispatchMenuItem(image: #imageLiteral(resourceName: "podIcon"), title: "Customer Pickup")
return [video1, video2]
}
}
extension DispatchViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return videos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let video = videos[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "DispatchMenuItemCell") as! DispatchMenuItemCell
cell.setVideo(video: video)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
print(indexPath)
if indexPath.row == 1{
self.performSegue(withIdentifier: "goToOpenManifests", sender: nil)
}else if indexPath.row == 2{
self.performSegue(withIdentifier: "goToCustomerPickupForm", sender: nil)
}
}
}
您可以在底部看到我的DidSelectRow
,但它不想打印出indexPath.row所以我假設所選擇的行現在以某種方式通過擴展傳遞。
有什么建議嗎? 我通常只是通過一個UITableViewController做到這一點,從來沒有遇到過這個問題,但是在這里的某個地方我似乎陷入困境。
由於您尚未設置委托,因此不會打印出indexPath。 僅實現該方法是不夠的, tableView
需要知道誰將實現委托方法。
委托和數據源之間存在差異。
tableView.dataSource
將幫助您獲取numberofSections,numberofRowsInASection,TableCell對象
tableView.delegate
將幫助您管理選擇和取消選擇。
override func viewDidLoad() {
super.viewDidLoad()
videos = createArray()
self.tableView.rowHeight = 65.0
tableView.dataSource = self
//Add this
tableView.delegate = self
}
您只需要在viewDid加載方法的第一行代碼下面添加,並改進您的代碼,如下所示。
tableView.delegate = self
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
print(indexPath)
if indexPath.row == 0 {
self.performSegue(withIdentifier: "goToOpenManifests", sender: nil)
}else if indexPath.row == 1 {
self.performSegue(withIdentifier: "goToCustomerPickupForm", sender: nil)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.