2015-07-02 9 views
21

में छुआ या क्लिक किया गया है मैं TableView में चयनित आइटम के index प्राप्त करने का प्रयास कर रहा हूं और उसके बाद कुछ गतिविधि शुरू कर रहा हूं। दुर्भाग्य से मुझे मिले समाधानों में से अधिकांश उद्देश्य-सी में हैं या काम नहीं करते हैं।तालिका दृश्य कक्ष का पता लगाने के लिए कैसे स्विफ्ट

विधि func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)cell लेबल मुद्रित नहीं है ..

किसी मेरी मदद कर सकते हैं?

 
import UIKit 
import ResearchKit 

class TaskListViewController: UIViewController, UITableViewDataSource { 

    let tasks=[("Short walk"), 
     ("Audiometry"), 
     ("Finger tapping"), 
     ("Reaction time"), 
     ("Spatial span memory") 
    ] 


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

    //return int how many rows 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return tasks.count 
    } 

    //what are the contents 

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

     var (testName) = tasks[indexPath.row] 
     cell.textLabel?.text=testName 
     return cell 
    } 

    // give each table section a name 

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

     return "Tasks" 

    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     let indexPath = tableView.indexPathForSelectedRow(); 

     let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell! 

     println(currentCell.textLabel!.text) 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 
} 

कुछ प्रयासों के बाद मैंने कोड को ट्यूटोरियल से अलग-अलग में बदल दिया। और यह भी काम नहीं करता है। अब मैं सोच रहा हूँ यह आईओएस सिम्युलेटर के साथ मुद्दा है ...

 
import UIKit 
import ResearchKit 

class TaskListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet 
    var tableView: UITableView? 
    var items: [String] = ["We", "Heart", "Swift"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.items.count; 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell:UITableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

     cell.textLabel?.text = self.items[indexPath.row] 

     return cell 
    } 

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

} 

उत्तर

28

आप सेल से मूल्य चाहते हैं तो आपको didSelectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    println(tasks[indexPath.row]) 
} 

टास्क में सेल को पुन: की जरूरत नहीं है होगा इस प्रकार:

let tasks=["Short walk", 
    "Audiometry", 
    "Finger tapping", 
    "Reaction time", 
    "Spatial span memory" 
] 

भी आप की जाँच करने के लिए है cellForRowAtIndexPath आप पहचानकर्ता सेट करना होगा।

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell 
    var (testName) = tasks[indexPath.row] 
    cell.textLabel?.text=testName 
    return cell 
} 

उम्मीद है कि यह मदद करता है।

+0

@ पॉविसून मुझे लगता है कि आपको सेलफोरो में समस्या है। अद्यतन उत्तर –

2

समस्या अपने आप हल कर लिया गया weheartswift

enter image description here

+3

जांचें क्या चयनित उत्तर वास्तव में समस्या को हल करता है? यदि यह समस्या को ठीक नहीं करता है, तो कृपया अपने उत्तर पर एक समाधान लिखें या आपके द्वारा पूछे गए प्रश्न को ठीक करने के लिए उचित उत्तर का चयन करें। सिर्फ इसलिए कि कोई मदद और संभावित समाधान प्रदान करता है, इसका मतलब यह नहीं है कि उनका उत्तर सही के रूप में चुना जाना चाहिए। कृपया इसे ठीक करें। – Devbot10

17

के ट्यूटोरियल का उपयोग स्विफ्ट 3.0 में

आप इसे विधि प्रतिनिधि के माध्यम से tableview की सेल के स्पर्श/क्लिक के लिए घटना पा सकते हैं । साथ ही, इस तरह के सेल के अनुभाग और पंक्ति मान पा सकते हैं।

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print("section: \(indexPath.section)") 
     print("row: \(indexPath.row)") 
} 
1

यह मेरे लिए अच्छा काम किया:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
 
     print("section: \(indexPath.section)") 
 
     print("row: \(indexPath.row)") 
 
    }

उत्पादन किया जाना चाहिए:

section: 0 
row: 0 
+3

आपका उत्तर डुप्लिकेट दिखाई देता है, क्योंकि यह उत्तर उपरोक्त उत्तर के समान दिखाई देता है https://stackoverflow.com/a/41583381/40867 – jdev

0
कुछ बातें की एक होने के लिए ...

जरूरत है कि

  1. दृश्य नियंत्रक प्रकार UITableViewDelegate

  2. दृश्य नियंत्रक didSelectRowAt समारोह में शामिल करने की जरूरत है का विस्तार करने की जरूरत है।

  3. तालिका दृश्य में दृश्य नियंत्रक को इसके प्रतिनिधि के रूप में असाइन किया जाना चाहिए।


नीचे एक जगह है जहाँ प्रतिनिधि बताए जगह ले (देखें नियंत्रक के अंदर) कर सकता है।

override func loadView() { 
    tableView.dataSource = self 
    tableView.delegate = self 
    view = tableView 
} 

और didSelectRowAt फ़ंक्शन का एक सरल कार्यान्वयन।

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print("row: \(indexPath.row)") 
} 
संबंधित मुद्दे