我很难通过自定义单元格中的按钮传递数据。所以我有一个自定义单元格,有一个按钮。在单元格的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
}
但是当我点击按钮时没有传递任何信息。目标视图控制器中的文本字段为空。
答案 0 :(得分:3)
按照以下步骤轻松完成任务。
首先在你的UITableViewCell
类中定义一个按钮属性,就像下面的函数..
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
添加此行
var editBtnClickFunction: (() -> Void)? = nil
在此行之后,在您的按钮单击操作中添加一点代码。
if let onButtonTapped = self. editBtnClickFunction {
onButtonTapped()
}
现在使用
进入主控制器UITableViewDataSource
和Delegate
在
之前添加其他代码cellForRowAt
功能中,在return cell
声明
cell. editBtnClickFunction = {
performSegue(withIdentifier: "EditPost", sender: indexPath.row)
}
现在是你的segue准备方法中的最终代码
if segue.identifier == "EditPost" {
if sender != nil {
let index : Int = Int(sender! as! String)
let vc = segue.destination as! EditPostViewController
let post = self.posts[index] 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
}
}
希望这会对你有所帮助
答案 1 :(得分:1)
按如下方式更改此行
cell.UserEdit.addTarget(self, action: #selector(EditClicked(sender:)), for: UIControlEvents.touchUpInside)
并按如下所示更改目标方法声明
@IBAction func EditClicked(sender: UIButton) -> Void {
let indexPath = self.selectedIndex
performSegue(withIdentifier: "EditPost", sender: indexPath)
}
尝试按上述方式更改代码。