2015-02-08 3 views
7

मुझे एक त्रुटि संदेश मिल रहा है "नीचे दिए गए वर्ग के साथ स्विफ्ट से Optional" अनपेक्षित करते समय अप्रत्याशित रूप से nil मिला। लाइन पर त्रुटि होती है:वैकल्पिक मूल्य स्विफ्ट को अनदेखा करते समय अनपेक्षित रूप से पाया गया शून्य

(cell.contentView.viewWithTag(1) as UILabel).text = object["firstName"] as? String 

मैं दुकानों

import UIKit 
    import Foundation 

    class dictionaryTableViewController: UIViewController,  UITableViewDelegate, UITableViewDataSource{ 

    var objects = NSMutableArray() 
    var dataArray = [["firstName":"Debasis","lastName":"Das","email":"[email protected]"],["firstName":"John","lastName":"Doe","email":"[email protected]"],["firstName":"Jane","lastName":"Doe","email":"[email protected]"],["firstName":"Mary","lastName":"Jane","email":"[email protected]"]] 

    @IBOutlet 
    var tableView: UITableView! 

    var items: [String] = ["We", "Heart", "Swift"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return dataArray.count;//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 
     let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as UITableViewCell 
     let object = dataArray[indexPath.row] as NSDictionary 
     (cell.contentView.viewWithTag(1) as UILabel).text = object["firstName"] as? String 
     (cell.contentView.viewWithTag(2) as UILabel).text = object["lastName"] as? String 
     return cell 
    } 

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

     println("You selected cell #\(indexPath.row)!") 

    } 

} 
+1

मुझे लगता है कि समस्या के साथ 'viewWithTag (_ :)' है, जो एक 'UIView रिटर्न' (वैकल्पिक 'UIView') क्या है?। यदि यह 'शून्य 'है, तो' text' संपत्ति पहुंच दुर्घटना का कारण बनती है। कोड को बदलने की कोशिश करें 'अगर टैग 1 = cell.contentView.viewWithTag (1) UILabel {...}' के रूप में दें और यह देखने के लिए वहां क्या होता है तो ब्रेकपॉइंट डालें। – Romain

उत्तर

20

आप एक कस्टम सेल लेआउट का उपयोग करना चाहते हैं, मैं

  1. सुझाव है

    उपवर्ग UITableViewCell:

    // CustomTableViewCell.swift 
    
    import UIKit 
    
    class CustomTableViewCell: UITableViewCell { 
    
        @IBOutlet weak var firstNameLabel: UILabel! 
        @IBOutlet weak var lastNameLabel: UILabel! 
    
    } 
    
  2. स्टोरीबोर्ड में तालिका दृश्य तैयार करें। स्टोरीबोर्ड में टेबलव्यू में सेल प्रोटोटाइप जोड़ें। डिजाइन करें कि सेल प्रोटोटाइप (जो भी लेबल आप चाहते हैं जोड़ना)।

  3. सेल प्रोटोटाइप के लिए स्टोरीबोर्ड पहचानकर्ता निर्दिष्ट करें जो भी आप चाहते हैं (MyCell अपने उदाहरण में)।

    enter image description here

  4. , आधार वर्ग निर्दिष्ट सेल प्रोटोटाइप होने के लिए, इस उदाहरण में CustomTableViewCell:

    enter image description here

  5. अपने सेल प्रोटोटाइप और अपने UITableViewCell उपवर्ग के बीच IBOutlet संदर्भ ऊपर हुक ।

    enter image description here

    सूचना IBOutlet संदर्भ के बाईं ओर ठोस गोलियों। यह इंगित करता है कि आउटलेट सही ढंग से झुका हुआ था।

  6. आपके डेटा स्रोत में cellForRowAtIndexPath लागू करें, उदा। स्विफ्ट 3 में:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell 
    
        let person = dataArray[indexPath.row] 
    
        cell.firstNameLabel.text = person["firstName"] 
        cell.lastNameLabel.text = person["lastName"] 
    
        return cell 
    } 
    

    या स्विफ्ट 2 में:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
        let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! CustomTableViewCell 
    
        let person = dataArray[indexPath.row] 
    
        cell.firstNameLabel.text = person["firstName"] 
        cell.lastNameLabel.text = person["lastName"] 
    
        return cell 
    } 
    
  7. ध्यान दें, सेल प्रोटोटाइप का उपयोग कर, तो आप नहींviewDidLoad में registerClass कॉल करना चाहते हैं। स्टोरीबोर्ड स्वचालित रूप से आपके कस्टम क्लास को आपके लिए पुन: उपयोग किए गए पहचानकर्ता के साथ पंजीकृत करेगा (आपके द्वारा चरण 3 और 4 में किए गए कार्यों के कारण)।

+0

धन्यवाद, यह सबसे अच्छा जवाब है! – Jhonny

+0

धन्यवाद, यह मुझे मिला क्योंकि मुझे कस्टम कोशिकाओं के लिए xibs बनाने के लिए उपयोग किया जाता है: 7. नोट, सेल प्रोटोटाइप का उपयोग करते समय, आप रजिस्टर क्लास को देखने के लिए कॉल नहीं करना चाहते हैं। स्टोरीबोर्ड स्वचालित रूप से आपके कस्टम क्लास को आपके लिए पुन: उपयोग किए गए पहचानकर्ता के साथ पंजीकृत करेगा (आपके द्वारा चरण 3 और 4 में किए गए कार्यों के कारण)। –

+2

मुझे एक त्रुटि है "घातक त्रुटि: वैकल्पिक मान को अनदेखा करते समय अनपेक्षित रूप से पाया गया शून्य" –

3

EDIT2 सेट कर रहे हैं 2 UILabels के साथ एक कस्टम सेल वर्ग में चिह्नित 1 और 2 है,: यह समाधान हो सकता है आपके इस प्रयोजन के लिए बेहतर है। इसके साथ आप अपनी खुद की UITableViewCell के लिए अपनी कक्षा बनाते हैं और इसका उपयोग पुन: उपयोग पैटर्न के साथ करते हैं। टिप्पणियाँ कोड में हैं:

class dictionaryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{ 

    // The data 
    var dataArray = [["firstName":"Debasis","lastName":"Das","email":"[email protected]"],["firstName":"John","lastName":"Doe","email":"[email protected]"],["firstName":"Jane","lastName":"Doe","email":"[email protected]"],["firstName":"Mary","lastName":"Jane","email":"[email protected]"]] 

    // The outlet to your tableview in the ViewController in storyboard 
    @IBOutlet var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Create a nib for reusing 
     let nib = UINib(nibName: "MyCell", bundle: nil) 
     // Register the nib for reusing within the tableview with your reuseidentifier 
     tableView.registerNib(nib, forCellReuseIdentifier: "MyCell") 

     // Set delegate and datasource to self (you can do that in interface builder as well 
     tableView.delegate = self 
     tableView.dataSource = self 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return dataArray.count; // return the number of datasets 
    } 

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

     // create a reusable cell with the reuse identifier you registered in viewDidLoad and cast it to MyCell 
     let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as MyCell 
     let object = dataArray[indexPath.row] as NSDictionary 

     // set the strings matching your array 
     cell.label1.text = object["firstName"] as? String 
     cell.label2.text = object["lastName"] as? String 

     // return the cell 
     return cell 

    } 

    // some example for the delegate 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     println("You selected cell #\(indexPath.row)!") 
    } 


} 


// class for you own UITableViewCell 
// has own xib file 
class MyCell: UITableViewCell { 

    // two outlets representing two labels in the xib file 
    @IBOutlet private weak var label1: UILabel! 
    @IBOutlet private weak var label2: UILabel! 
} 

इसके लिए आपको सही नाम के साथ एक xib-फ़ाइल बनाने के लिए (इस मामले MyCell में) काम करने के लिए।

enter image description here

यह कस्टम दृश्य और दुकानों के वर्ग सेट करने के लिए बहुत महत्वपूर्ण है: यह इस के समान कुछ करना चाहिए।

------------------------------------- अन्य समाधान ------- -------------------------------

संपादित करें: जैसा कि मैंने इसे देखा है, आप अपनी खुद की कक्षा शुरू नहीं करते हैं ' MyCell 'लेकिन ऐप्पल की कक्षा' UITableViewCell 'reuseIdentifier' MyCell 'के साथ। तो यह UITableViewCell का उपयोग करने का समाधान है:

लाइन (cell.contentView.viewWithTag(1) as UILabel) केवल एक विशिष्ट वर्ग के लिए एक कलाकार नहीं है, यह वैकल्पिक (UIView) को भी खोल रहा है। आप इसे '!' के साथ अन्य मामलों में करते हैं। तो समस्या यह है: आप UITableViewCell का एक उदाहरण बना रहे हैं, जो एक क्लास बनाया गया है। आप विशिष्ट टैग के साथ उस दृश्य के अंदर सबव्यूव प्राप्त करना चाहते हैं। आप कैसे जानते हैं कि सेब ने उन्हें ये टैग दिए हैं? क्या आप वास्तव में चाहते हैं इस तरह एक विशेष शैली के साथ UITableViewCell का उदाहरण बनाकर और textLabels के मूल्यों को निर्धारित है:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // Create an instance of UITableViewCell. Style is .Subtitle but could be others as well 
     var cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as? UITableViewCell 
    let object = dataArray[indexPath.row] as NSDictionary 

    // set the strings matching your array 
    cell?.textLabel?.text = object["firstName"] as? String 
    cell?.detailTextLabel?.text = object["lastName"] as? String 

    // return the cell 
    return cell! 
    } 
+0

वह 'माईसेल' को तत्काल कर रहा है - सेब की कक्षा नहीं - इसलिए उसके टैग वहां हो सकते हैं :) –

+0

हाँ मैंने अपने टैग सेट किए हैं, लेकिन फिर भी यह काम करता है तो धन्यवाद! – Jhonny

+1

उन्होंने क्लास UITableViewCell के लिए 'reyuseIdentifier के रूप में' MyCell 'सेट किया है, इसलिए वह UITableViewCell को प्रारंभ कर रहा है। अगर यह काम करता है तो कृपया उत्तर को सही के रूप में जांचें ताकि अन्य लोग जो इस समस्या की खोज कर रहे हैं, जवाब देख सकें .. – Ben

0

सभी मैं करना पड़ा था:

cell.subviews[0].subviews[0].viewWithTag(//your tag//) 
संबंधित मुद्दे