简体   繁体   English

迅捷:Table View Cell执行到两个不同目的地的隔离

[英]Swift: Table View Cell perform segue to two different destinations

My question: I wanna perform segue to two different destinations in a table view cell. 我的问题:我想在表视图单元格中对两个不同的目的地执行segue。 One is directing destination "A" by clicking the cell which handled by didSelectRowAt, Another one is directing to destination "B" by clicking the button inside the cell, for example, the labelName. 一种是通过单击didSelectRowAt处理的单元格来定向目标“ A”, 另一种是通过单击单元格内的按钮(例如,labelName)定向到目标“ B”。

For destiation "B", I have try to use (btn.addTarget), but it cannot add parameters, so the destinatin view controller do not know the which cell was being clicked. 对于目标“ B”,我尝试使用(btn.addTarget),但无法添加参数,因此目标视图控制器不知道单击了哪个单元格。

Any suggestion? 有什么建议吗?

you can use button.tag and assign the tag of every button with your indexpath.row 您可以使用button.tag并将每个按钮的标签分配给indexpath.row

button.tag = indexPath.row
button.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)

func btnClick(sender:UIButton) {
    let buttonRow = sender.tag
}

for swift 4 : 快速4:

  button.addTarget(self, action: #selector(händlerButtonTapped), for: .touchUpInside)

    @objc func händlerButtonTapped(sender:UIButton) {
       let buttonRow = sender.tag
    }

You can Get the indexPath of your selected TableviewCell in your btnClick function by using this. 您可以使用此方法在btnClick函数中获取所选TableviewCell的indexPath。

let index = self.tableview.indexPathForSelectedRow

Now by doing this you can solve this problem you mentioned. 现在,通过这样做您可以解决您提到的问题。

the destinatin view controller do not know the which cell was being clicked destinatin视图控制器不知道单击了哪个单元格

Here is my cellForRowAt: 这是我的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM