使用TableViewCell IndexPath值执行segue

时间:2018-05-19 07:02:24

标签: ios swift

我在TableViewCell中有一个UIButton,我想在点击UIButton时执行一个segue,但我需要在执行segue时知道IndexPath.row值。这里有更多细节:

enter image description here

执行segue的代码块:

}else if(segue.identifier == "toChat"){
            let chatController = segue.destination as! ChatViewController
            chatController.employeeSender = self.employeeData
            //Currently I have tried this, but indexPath comes nil
            if let indexPath = tableView.indexPathForSelectedRow {
            chatController.employeeReceiver = (self.searchController!.isActive) ? self.searchResults[indexPath.row] : self.employeeArray[indexPath.row]
                print("Receiver Data\(self.employeeArray[indexPath.row].alias)")
            }

        }

谢谢,

4 个答案:

答案 0 :(得分:0)

您有两个选择

1)轻松

您可以在cellForRowAtindexPath的按钮中添加标记,当您点击按钮事件时,您可以使用该按钮创建indexPath

2)创建您UIButton的子类,并在其中包含一个IndexPath属性。现在,当点击按钮时,您可以访问button.indexPath

并在cellForRow中指定indexpath

不要忘记将事件传递给带有委托或clouser的viewcontroller

希望它有用

答案 1 :(得分:0)

试试这个:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! yourcustomcell
        cell.yourbutton.tag = indexPath.row
        cell.yourbutton.addTarget(self, action: #selector(self.chatbutton), for: .touchUpInside)
        return cell
 }

// Button Click
 @objc func chatbutton(_ sender: UIButton) {
        self.performSegue(withIdentifier: "toChat", sender: sender.tag)
 }


 // MARK: - prepare segue

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "toChat" {
                let chatController = segue.destination as! ChatViewController
                chatController.employeeSender = self.employeeData
                if let row = sender as? Int  {
                    chatController.employeeReceiver = (self.searchController!.isActive) ? self.searchResults[row] : self.employeeArray[row]
                }
            }
    }

答案 2 :(得分:0)

您还可以使用以下扩展名获取单元格的indexPath的值。

public extension UITableView {

    func indexPathForView(_ view: UIView) -> IndexPath? {
        let center = view.center
        let viewCenter = self.convert(center, from: view.superview)
        let indexPath = self.indexPathForRow(at: viewCenter)
        return indexPath
    }
}

并在按钮

的@IBAction方法中获取indexPath
let indexPath = tableView.indexPathForView(sender)

答案 3 :(得分:0)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! yourcustomcell
        cell.button.tag = indexPath.row
        cell.button.addTarget(self, action: #selector(self.buttonTapped), for: .touchUpInside)
        return cell
 }

// Button Click
 @objc func buttonTapped(_ sender: UIButton) {
        pushToChatController(self.employeeReceiverArray[sender.tag])
 }


 func pushToChatController(aEmployeeReceiver: EmployeeReceiver) {
        let chatController: ChatViewController = UIStoryboard.chatStoryboard().instantiateViewController(withIdentifier: kChatViewControllerStoryboardId) as! ChatViewController
        chatController. employeeReceiver = aEmployeeReceiver
        navigationController?.pushViewController(chatController, animated: true)
  }

//注意:     class func chatStoryboard() - > UIStoryboard {         return UIStoryboard(name:“Chat”,bundle:nil)     }

让kChatViewControllerStoryboardId =“kChatViewControllerStoryboardId” //故事板标识符