我很难通过自定义单元格中的按钮传递数据。所以我有一个自定义单元,它有一个按钮。在细胞的TableViewCell,按钮被宣布自定义单元格中的按钮传递信息
@IBOutlet var UserEdit: RoundButton!
然后在具有自定义单元格中的ViewController,按钮被赋予一个动作。
@IBAction func EditClicked(_ sender: UIButton) -> Void {
let indexPath = self.selectedIndex
performSegue(withIdentifier: "EditPost", sender: indexPath)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as! ProfileTableViewCell
cell.UserEdit.addTarget(self, action: #selector(EditClicked), for: UIControlEvents.touchUpInside)
cell.UserEdit.tag = indexPath.row
print(indexPath.row)
cell.UserEdit.isUserInteractionEnabled = true
}
而且SEGUE完美的作品,不过,我会想通过该按钮被点击的细胞的信息,因此我这样做:
override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "Hello", let chatVc = segue.destination as? SendMessageViewController else {
return
}
chatVc.senderId = self.loggedInUser?.uid
chatVc.receiverData = self.userpicuid
chatVc.senderDisplayName = self.userpicuid
chatVc.username = self.username
if segue.identifier == "EditPost" {
if let indexPath = sender as? IndexPath {
let vc = segue.destination as! EditPostViewController
let post = self.posts[indexPath.row] as! [String: AnyObject]
let Booked = post["title"] as? String
let Authors = post["Author"] as? String
let ISBNS = post["ISBN"] as? String
let Prices = post["Price"] as? String
let imageNames = post["image"] as? String
vc.Booked = Booked
vc.Authors = Authors
vc.ISBNS = ISBNS
vc.Prices = Prices
vc.imageNames = imageNames
print(indexPath.row)
}
}}
而在目标视图控制器,我有这个:
@IBOutlet var Author: UITextField!
@IBOutlet var Price: UITextField!
@IBOutlet var Books: UIImageView!
@IBOutlet var ISBN13: UITextField!
@IBOutlet var ISBN10: UITextField!
@IBOutlet var CoverType: UITextField!
@IBOutlet var Details: UITextField!
var Booked: String?
var Authors: String?
var ISBNS: String?
var Prices: String?
var imageNames: String?
override func viewDidLoad() {
super.viewDidLoad()
self.BookTitle.text = self.Booked
self.Author.text = self.Authors
self.ISBN10.text = self.ISBNS
self.Price.text = self.Prices
}
但是,当我点击按钮时没有信息被传递。目标视图控制器中的文本字段为空。
你怎么了'selectedIndex' –
您可以选择使用协议委托或关闭传递数据。 –
@安布。Karthik var selectedIndex:IndexPath? – juelizabeth