我很難通過自定義單元格中的按鈕傳遞數據。所以我有一個自定義單元,它有一個按鈕。在細胞的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