2014-11-12 11 views
12

के भीतर किसी बटन से एक सेग्यू निष्पादित करना मैं कस्टम UITableViewCell के भीतर एक बटन से एक सेग्यू करना चाहता हूं, लेकिन मुझे नहीं पता कि जब मैं उस बटन को दबाता हूं तो सेल की सामग्री तक कैसे पहुंचना है। मुझे एहसास है कि यह didSelectRowAtIndexPath के माध्यम से पूरा किया जा सकता है, हालांकि मेरे पास यह तरीका एक और फ़ंक्शन कर रहा है, और मैं टेबल सेल के अंदर बनाए गए बटन का उपयोग करना चाहता हूं।एक कस्टम UITableViewCell

यहाँ मेरी प्रदर्शन segue विधि मैं कुछ समस्या आ रही है:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{ 
    if segue.identifier == "showComments" 
    { 
     let vc:CommentOnPostViewController = segue.destinationViewController as CommentOnPostViewController 

     var buttonPosition:CGPoint = sender?.convertPoint(CGPointZero, toView: self.feed) as CGPoint! 
     var path:NSIndexPath = self.feed.indexPathForRowAtPoint(buttonPosition) as NSIndexPath! 

     var postToCommentOn:PFObject = feedList[path.row] as PFObject 
     vc.post = postToCommentOn as PFObject 
    } 
} 

जब यह प्रदर्शित मैं कक्ष में बटन को टैग, और यह एक लक्ष्य कार्रवाई दे:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    // There is code that goes here for creating and displaying the cell. 

    cell.commentButton.tag = indexPath.row 
    cell.commentButton.addTarget(self, action: "addComment", forControlEvents: UIControlEvents.TouchUpInside) 
} 

यहाँ वह बटन है जिसे बटन दबाते समय कहा जाता है:

func addComment() 
{ 
    self.performSegueWithIdentifier("showComments", sender: self) 
} 

किसी भी मदद की सराहना की जाती है। धन्यवाद!

+0

क्या आप स्वयं को नहीं भेज सकते हैं, लेकिन बटन या सेल प्रेषक के रूप में? –

+0

इस तरह कुछ करने के लिए एक प्रतिनिधि का उपयोग करें।प्रतिनिधि को कस्टमसेल क्लास पर और टेबलव्यू कंट्रोलर पर परिभाषित किया जाएगा प्रत्येक सेल के प्रतिनिधि को सेट करें ... UITableViewController पर प्रतिनिधि के तरीके को परिभाषित करें जो आपको स्वयं कॉल करेगा। performSegueWithIdentifier ... जिज्ञासा से बाहर क्या आप "didSelectRowAtIndexPath" के साथ क्या कर रहे हैं? –

उत्तर

30

विधि के साथ एक प्रोटोकॉल, कि CustomCell के प्रतिनिधि द्वारा बुलाया जाएगा, अपने TableViewController पर परिभाषित बनाएं

//Pass any objects, params you need to use on the 
//segue call to send to the next controller. 

protocol MyCustomCellDelegator { 
    func callSegueFromCell(myData dataobject: AnyObject) 
} 

अब आप अपने UITableViewController पर प्रोटोकॉल का उपयोग करें

class MyTableViewController : UITableViewController, MyCustomCellDelegator { 


//The usual Defined methods by UIViewController and UITableViewController 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    //Set the CustomCell new Delegate 
    var cell = tableView.dequeueReusableCellWithIdentifier(customIdentifier) as MyCustomCell 


    cell.delagete = self 

    return cell 

} 


//MARK: - MyCustomCellDelegator Methods 

func callSegueFromCell(myData dataobject: AnyObject) { 
    //try not to send self, just to avoid retain cycles(depends on how you handle the code on the next controller) 
    self.performSegueWithIdentifier("showComments", sender:dataobject) 

} 

} 

पर प्रतिनिधि को परिभाषित करें अपने कस्टम सेल और आपके नए बटन के अंदर कॉल प्रतिनिधि समारोह।

class MyCustomCell : UITableViewCell { 

     var delegate:MyCustomCellDelegator! 
     @IBOutlet weak var myButton:UIButton 



    @IBAction func buttonPressed(sender:AnyObject){ 
      var mydata = "Anydata you want to send to the next controller" 
      if(self.delegate != nil){ //Just to be safe. 
      self.delegate.callSegueFromCell(mydata) 
      } 
    } 
} 

उम्मीद है कि यह आपके कोड में समझने और कार्यान्वित करने के लिए यह स्पष्ट और स्पष्ट हो सकता है।

+0

मैं स्टोरीबोर्ड का उपयोग कर सेगू कैसे बना सकता हूं? मैं कहां से CTRL + खींचें? –

+0

@ क्रिस्टोफर फ्रांसिस्को CTRL + उस नियंत्रक से खींचें जिसे आप गंतव्य नियंत्रक से ले जाना चाहते हैं और इसे आपको कुछ विकल्प दिखाना चाहिए जैसे: शो, पुश, मोडल कुछ ऐसा करें। और बस। ओह और नव निर्मित सीगू में पहचानकर्ता जोड़ना सुनिश्चित करें। –

+0

आप प्रोटोकॉल कैसे बनाते हैं? स्पष्ट रूप से दिखाए गए कोड की नकल करें, लेकिन जिसमें ViewController? या प्रोटोकॉल स्टैंडअलोन हैं? यदि स्टैंडअलोन है, तो क्या मैं एक पूरी नई स्विफ्ट फ़ाइल बना सकता हूं? –

0

मुझे एक नए वीसी में एक गतिशील सेल में एक बटन के साथ डेटा पास करने के लिए एक तेज उत्तर मिला।

  1. एक tableview, एक कस्टम सेल (आपका बनाई वर्ग के साथ सेल को जोड़ने और एक सेल पहचानकर्ता सेट) की स्थापना और एक नए कुलपति बना सकते हैं और एक segue (भी segue के लिए एक पहचानकर्ता सेट) के माध्यम से इसे कनेक्ट अपने वर्तमान TableViewController को

  2. आउटलेट (cellButton) TableViewController-क्लास

    में

    class CustomCell: UITableViewCell { 
        @IBOutlet weak var cellButton: UIButton! 
    
    
        override func awakeFromNib() { 
         super.awakeFromNib() 
        } 
    
        override func setSelected(_ selected: Bool, animated: Bool) { 
         super.setSelected(selected, animated: animated) 
        } 
    } 
    
  3. CellForRowAt समारोह के साथ अपने सेल की स्थापना

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell 
    
        //...Do stuff for your cell 
    
        cell.cellButton.tag = indexPath.row //or value whatever you want (must be Int) 
        cell.cellButton.addTarget(self, action: #selector(TableViewController.buttonTapped(_:)), for: UIControlEvents.touchUpInside) 
    } 
    
  4. बटन विधि है कि बटन

    func buttonTapped(_ sender:UIButton!){ 
        self.performSegue(withIdentifier: "customSegueIdentifier", sender: sender) 
    } 
    
  5. का लक्ष्य द्वारा कहा जाता है की स्थापना की segue समारोह के लिए तैयार

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
        if(segue.identifier == "customSegueIdentifier") { 
         if let destination = segue.destination as? YourNewViewController { 
    
          if let button:UIButton = sender as! UIButton? { 
           print(button.tag) //optional 
           destination.valueViaSegue = button.tag 
          } 
         } 
        } 
    } 
    

आशा इस कोड को तुम लोगों में मदद मिलेगी। कोडिंग रखें;)

+0

धन्यवाद! मैं इस समाधान को संकलित करने में आपके इनपुट और आपके प्रयास की सराहना करता हूं! क्या आप इस बारे में थोड़ा सा समझाएंगे कि यह कैसे तेज है? – user3353890

संबंधित मुद्दे