[英]UIButton inside Table Cell doesn't segue to a View Controller in Swift
[英]Swift: Table View Cell perform segue to two different destinations
我的問題:我想在表視圖單元格中對兩個不同的目的地執行segue。 一種是通過單擊didSelectRowAt處理的單元格來定向目標“ A”, 另一種是通過單擊單元格內的按鈕(例如,labelName)定向到目標“ B”。
對於目標“ B”,我嘗試使用(btn.addTarget),但無法添加參數,因此目標視圖控制器不知道單擊了哪個單元格。
有什么建議嗎?
您可以使用button.tag並將每個按鈕的標簽分配給indexpath.row
button.tag = indexPath.row
button.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
func btnClick(sender:UIButton) {
let buttonRow = sender.tag
}
快速4:
button.addTarget(self, action: #selector(händlerButtonTapped), for: .touchUpInside)
@objc func händlerButtonTapped(sender:UIButton) {
let buttonRow = sender.tag
}
您可以使用此方法在btnClick函數中獲取所選TableviewCell的indexPath。
let index = self.tableview.indexPathForSelectedRow
現在,通過這樣做您可以解決您提到的問題。
destinatin視圖控制器不知道單擊了哪個單元格
這是我的cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! contentTableViewCell
let content: contentModel
content = contentList[indexPath.row]
contentViewController().getUserProfilePic(userID:content.userID!)
cell.detailLabelName.text = content.name
cell.detailLabelTime.text = content.time
cell.detailTextViewContent.text = content.content
if UserDefaults.standard.data(forKey:content.userID!) != nil {
cell.detailImageSelfie.image = UIImage(data:UserDefaults.standard.data(forKey:content.userID!)!)
} else {
cell.detailImageSelfie.image = #imageLiteral(resourceName: "defaultIcon")
}
return cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.