2015-03-15 9 views
16

नहीं कहा जा रहा है IOS विकास के लिए नया और तालिका में सेल चयन को संभालने में समस्या हो रही है। जब भी मैं चुनता हूं, विधि नीचे नहीं जा रही है - कोई विचार क्यों?स्विफ्ट UITableView didSelectRowAtIndexPath को

मेरे परियोजना संरचना है: देखें नियंत्रक -> देखें -> टेबल देखें

नीचे कोड विधि कॉल को दर्शाता है। दूसरों को कोई समस्या नहीं कहा जाता है! मुझे पता है कि टच काम कर रहा है क्योंकि सफलतापूर्वक रीफ्रेश हो जाता है और सेल पर क्लिक करने पर यह हाइलाइट हो जाता है।

import UIKit 

class ViewController: UIViewController, UITableViewDelegate 
{ 

    let blah = ["blah1"] 

    //How many sections are in the table? 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    //How many rows? (returns and int) 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return blah.count 
    } 

    //table contents for each cell? 
    //Each time this is called it'll return the next row and thus build a table... 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     print("Populating each cell of table view!\n") 
     tableView.rowHeight = 80.0 
     var cell = UITableViewCell() 

     var(a) = blah[indexPath.row] 
     var image : UIImage = UIImage(named: a)! 
     cell.imageView.image = image 

     return cell 
    } 



    //Code Cell Selected 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 
     println("You selected cell #\(indexPath.row)!") 

    } 


    func tableView(tableView: UITableViewDelegate, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    print("wananananaanan") 
    println("You deselected cell #\(indexPath.row)!") 

    } 




    override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view, typically from a nib. 

    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 
} 
+2

में ऊपर प्रतिनिधि विधि का उपयोग करें? –

उत्तर

28

आप ViewController में tableView के लिए एक @IBOutlet सेट और के रूप में यह delegate है स्थापित करने के लिए और dataSource आप के लिए डेटा tableView में बदलाव के लिए एक प्रतिक्रिया देख सकते हैं।

कुछ इस तरह:

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.delegate = self 
    self.tableView.dataSource = self 
} 

और भी UITableViewDataSource प्रोटोकॉल लागू करता है।

या आप इंटरफ़ेस बिल्डर में भी यह प्रतिनिधि और dataSource रूप ViewController सेट कर सकते हैं और इसके बाद के संस्करण की तरह कोड में मैन्युअल रूप से सेट करने के लिए से बचने के (अधिक मुझे लगता है कि ऐसा करने के लिए आसान)। आप पर निर्भर है।

मुझे उम्मीद है कि यह आपकी मदद करेगा।

+0

या 'UIViewController' subclassing के बजाय,' UITableViewController' का उपयोग करें - फिर आपको मुफ्त में बहुत सारी कार्यक्षमता मिलती है। उदाहरण के लिए आपके यूज़र कंट्रोलर को इंगित करने वाले प्रतिनिधियों के साथ 'UITableView' पहले से ही आपके लिए सेटअप हो जाएगा। –

+0

हां निश्चित रूप से @Stefan की टिप्पणी भी मान्य है! –

+0

धन्यवाद, यह पूरी तरह से काम किया! (मैं व्यू कंट्रोलर को प्रतिनिधि आउटलेट के रूप में सेट करने के त्वरित विकल्प के साथ गया था। – Mark

5

मैं एक ही मुद्दे का सामना करना पड़ा जब दो समान कोड उदाहरण हैं, जहां एक अच्छी तरह से काम कर रहा था और अन्य बुला नहीं किया गया था didSelectRowAtIndexPath

इस मुद्दे को हल करने के लिए दो संभव तरीके पर एक नजर डालें तुलना में:

1) में कोड में ही:।

@IBOutlet weak var table: UITableView! 

override func viewDidLoad() { 
    table.delegate = self 
    table.dataSource = self 
//data source might be already set if you see contents of the cells 
//the main trick is to set delegate 
} 

2) स्टोरीबोर्ड या दस्तावेज़ को रेखांकित करें (जो मेरे मामले कारण स्टोरीबोर्ड परिवर्तन में समस्या .swift नियंत्रक वर्गों में दिखाई दे रहे हैं नहीं था का उपयोग करते हुए

ओपन दस्तावेज़ को रेखांकित करें और Control + Press अपने TableView (पीला वृत्त पर दाएं) आप दो दुकानों कहा जाता दिखाई देगा "delegate" और "dataSource" उन्हें 1 1 से युक्त ViewController पर खींचें

यह है कि!

1

आपको इसका उपयोग करना होगा: सबसे पहले देखें कि आप क्या विस्तार कर रहे हैं और फिर तालिका दृश्य विधि का उपयोग करें।

class YourViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var mUITableView: UITableView! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    // We need to tell to UITableView that we will add the data by ourselves 
    self.mUITableView.delegate = self 
    self.mUITableView.dataSource = self 

    // Register the UITableViewCell class with the tableView 
    self.mUITableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentifier) 
    // Setup table data 
    getEvents() 

    self.mUITableView.allowsSelection = true 

} 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return tableData.count 
    } 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     // here to create you cell view 

    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     print("You selected cell #\(indexPath.row)!") 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "subtitleCell") 
     cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 
     cell.textLabel?.text = "\(tableData[indexPath.row].name) - (\(tableData[indexPath.row].eventStateId))" 
     cell.detailTextLabel?.text = tableData[indexPath.row].lastUpdate 
     return cell 
    } 



} 
15

स्विफ्ट 3

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     // Do here 
    } 

आप अपने आप को के रूप में मेज विचारों को सौंपने स्थापित किया था तेजी से 3

+0

केवल यह समाधान समाधान के लिए कुछ घंटों के बाद भी यहां काम करता है। धन्यवाद – guijob

+0

साथ ही ओवरराइड जोड़ें – Stingalingaling

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