2017-06-19 92 views
0

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

但是,當我點擊按鈕時沒有信息被傳遞。目標視圖控制器中的文本字段爲空。

+1

你怎麼了'selectedIndex' –

+1

您可以選擇使用協議委託或關閉傳遞數據。 –

+0

@安布。Karthik var selectedIndex:IndexPath? – juelizabeth

回答

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() 
    } 

在你的主控制器現在就從你正在使用UITableViewDataSourceDelegate

其中裏面你cellForRowAt功能您return cell語句前加上一個這樣的其它代碼

cell. editBtnClickFunction = { 
    performSegue(withIdentifier: "EditPost", sender: indexPath.row) 
} 

現在,它在你的賽格瑞最終代碼編制方法

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 
     } 
    } 

希望這將幫助你

+0

它可悲的沒有工作,但感謝您的努力 – juelizabeth

+0

@juelizabeth你能告訴我你在哪裏卡住在那 –

+0

我確實做了你發佈的內容,但是我得到和我的問題一樣的結果。沒有數據通過,當我嘗試檢查數據是否通過打印(預訂)傳遞時,它什麼都不打印 – juelizabeth

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) 


} 

嘗試改變如上述的代碼。

+0

我做了,但它仍然沒有工作,我包括一個打印語句和selectedRow = 0(我只有一個單元格) – juelizabeth

+0

嘗試刪除@IBAction函數定義(可能聽起來很愚蠢,但嘗試) – jegadeesh

+0

它沒有做出改變。我更新了我的'override public func prepare(for segue:UIStoryboardSegue,sender:Any?){'在我的問題中,因爲它們是它的其他細節,我不確定這是什麼使細節繼續工作 – juelizabeth

相關問題