2015-10-18 7 views
6

में मैं UITableViewController पर स्विफ्ट 2 में accessoryButtonTappedForRowWithIndexPath: को लागू करने का प्रयास कर रहा हूं।सहायक को कार्यान्वित करनाबटनटैप्डफॉररोविथ इंडेक्सपैथ: स्विफ्ट 2

जैसा कि मैंने नीचे बताया है, मुझे लगता है कि जब मैं disclosureIndicator बनाता हूं, तो मुझे कुछ याद आ रहा है, लेकिन मुझे नहीं पता कि क्या। यह कोड से खींचा जाता है, लेकिन मेरी लक्षित कार्रवाई नहीं कहा जाता है। छी!

इस प्रोग्रामेटिक रूप से करने के लिए, मेरी समझ है कि मुझे वापस आने से पहले cellForRowAtIndexPath में विवरण प्रकटीकरण सूचकांक जोड़ने की आवश्यकता है। मुझे लगता है कि इस प्रकार कर रहा हूँ:

// Create disclosure indicator button in the cell 
let disclosureIndicatorButton = UIButton(type: UIButtonType.DetailDisclosure) 
disclosureIndicatorButton.addTarget(self, action: "disclosureIndicatorPressed:event:", forControlEvents: UIControlEvents.TouchUpInside) 
customCell.accessoryType = .DisclosureIndicator 

मेरे कोड में, detailDisclosure शेवरॉन तैयार हो जाता है, लेकिन लक्ष्य कार्रवाई विधि मैं इसे करने के लिए सौंपा बुलाया प्राप्त करता है।

func disclosureIndicatorPressed(sender: UIButton, event: UIControlEvents) { 
    print("disclosure button pressed") 
    // convert touches to CGPoint, then determine indexPath 
    // if indexPath != nil, then call accessoryButtonTappedForRowWithIndexPath 
} 

अंत में accessoryButtonTappedForRowWithIndexPath segue, जो मैं कर सकते हैं प्रदर्शन करने के लिए कोड में शामिल हैं:

तब मैं बटन के लिए कोई हैंडलर बनाने के लिए जब यह दबाया है की जरूरत है। मैं क्या खो रहा हूँ?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:accessoryButtonTappedForRowWithIndexPath:

उत्तर

12

सुनिश्चित नहीं हैं कि तुम क्यों कोड में ऐसे ही प्रकटीकरण बटन सूचक जोड़ रहे हैं।

क्या आप देख रहे हैं बस 2 चरणों वाली प्रक्रिया है -

चरण 1: सेल पर सही accessoryType जोड़ें:

cell.accessoryType = .DetailDisclosureButton 

चरण 2: कार्यवाही करते बटन द्वारा उपयोग किया जाता है accessoryButtonTappedForRowWithIndexPath फ़ंक्शन बनाना:

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) { 
    doSomethingWithItem(indexPath.row) 
} 
+0

यह मेरी समस्या थी। 'प्रकटीकरण इंडिकेटर' सिर्फ आंख कैंडी है, खींचा जाने के अलावा कुछ और नहीं करता है। 'DetailDisclosureButton' वास्तव में इसके साथ जुड़ी कार्रवाई कर सकती है। मुझे स्क्वायर दूर करने के लिए धन्यवाद। – Adrian

+1

खुशी हुई यह मदद की! चीयर्स :)! – Abhinav

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