2014-06-28 11 views
20

अगर मैं uncommentस्विफ्ट में dequeueReusableCellWithIdentifier का उपयोग कैसे करें?

tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) 

मैं लाइन

let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 

कि कहते हैं UITableView? does not have a member named 'dequeueReusableCellWithIdentifier'

अगर मैं खोलने tableview तो त्रुटि दूर चला जाता है पर एक त्रुटि मिलती है, लेकिन ऑब्जेक्टिव-सी हम में सामान्य रूप से जांच करेगा कि सेल मौजूद है या नहीं, और यदि यह नहीं है तो हम एक नया बनाते हैं। स्विफ्ट में, चूंकि बॉयलरप्लेट प्रदान किया गया है let कीवर्ड का उपयोग करता है और एक वैकल्पिक को अनचाहे करता है, तो अगर यह शून्य है तो हम इसे पुन: असाइन नहीं कर सकते हैं।

स्विफ्ट में dequeueReusableCellWithIdentifier का उपयोग करने का उचित तरीका क्या है?

+0

सेल के बारे में नहीं के बराबर हो सकता है का उपयोग करें। इस विधि के साथ नहीं। दस्तावेज़: "UITableViewCell ऑब्जेक्ट संबंधित पुन: उपयोग पहचानकर्ता के साथ। यह विधि हमेशा एक मान्य सेल" –

+0

मुझे भ्रमित कर देती है क्योंकि मुझे ओबीजेसी दिनों को याद है जहां आपने पुन: उपयोग करने के लिए कोई सेल नहीं होने पर शून्य के लिए चेक किया था, और यदि नील ने नया बनाया 'reuseIdentifier' के साथ सेल। मैंने फिर से वर्तमान ऐप्पल दस्तावेज़ को पढ़ा और यह कहता है, "यह विधि किसी मौजूदा सेल को अस्वीकार करती है यदि कोई उपलब्ध है या आपके द्वारा पहले पंजीकृत क्लास या एनआईबी फ़ाइल का उपयोग करके एक नया बनाता है।" तो, समारोह अब हमारे लिए दोनों करता है, जो अच्छा है। हालांकि इस चर्चा को देखें, 2 डेक्यू कॉल हैं - एक वैकल्पिक विकल्प देता है, दूसरा नहीं: [लिंक] (https://www.natashatherobot.com/ios-using-the-wrong-dequeuereusablecellwithidentifier/) –

उत्तर

32

आप परोक्ष विधि के लिए मानकों को खोलने कर सकते हैं और यह भी निम्नलिखित संक्षिप्त कोड देने के लिए dequeueReusableCellWithIdentifier का परिणाम डाली: कोशिका प्रकार तालिका से पहले तालिका दृश्य के साथ पंजीकृत नहीं किया गया है

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

    //configure your cell 

    return cell 
} 
+3

यह वह उत्तर है जिसके साथ मैं भी कर सकता हूं - फ़ंक्शन परिभाषा में '?' के बजाय '!' (स्पष्ट रूप से अनचाहे वैकल्पिक के लिए) का उपयोग करने वाला प्रमुख अंतर (केवल उन लोगों के लिए इंगित करना जो तुरंत नोटिस नहीं करते हैं अंतर)। मुझे यह भी लगता है कि एक्सकोड 6 में ऐप्पल द्वारा प्रदान किया गया बॉयलरप्लेट इस परिभाषा को भी इसके साथ बदल देगा। – JuJoDi

5

आप tableView चर खोलने में के रूप में यह एक वैकल्पिक

if let realTableView = tableView { 
    let cell = realTableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 
    // etc 
} else { 
    // tableView was nil 
} 

है की क्या ज़रूरत है या आप 'ऑब्जेक्टिव-सी में हम करेंगे के बारे में अपने प्रश्न का द्वारा

tableView?.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 

यह छोटा कर सकते हैं जवाब में आम तौर पर जांचें कि सेल मौजूद है या नहीं, और यदि यह नहीं है तो हम एक नया बनाते हैं, dequeueReusableCellWithIdentifier हमेशा एक सेल लौटाता है (जब तक आप इस पहचानकर्ता के लिए कक्षा या निब पंजीकृत करते हैं), तो आपको इसकी आवश्यकता नहीं है एक नया निर्माण करने के लिए।

+0

डेक्यू रीयूसेबलसेलविथ इंडेंटिफायर हमेशा नहीं होता एक सेल, सीएफ दस्तावेज – greg3z

+0

क्षमा करें, मेरा मतलब है कि अगर आप इसे सही तरीके से सेट अप करते हैं तो मेरा हमेशा एक सेल लौटाता है।मैंने इसे स्पष्ट करने के लिए संपादित किया है – iain

+0

यदि आप इस दृष्टिकोण का उपयोग CellForAtIndexPath में करते हैं, तो आप सेल कैसे वापस करते हैं? – senty

24

private let cellReuseIdentifier: String = "yourCellReuseIdentifier" 

// MARK: UITableViewDataSource 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) 
    if (cell == nil) { 
    cell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:cellReuseIdentifier) 
    } 
    cell!.textLabel!.text = "Hello World" 
    return cell! 
} 
+0

स्विफ्ट 2.0 में चेतावनी देता है: 'UITableViewCell' से 'UITableViewCell' तक सशर्त कास्ट हमेशा –

+0

सफल होता है बस 'as?' – Zorayr

+0

हाँ को हटाने की आवश्यकता है, और मुझे लगता है कि अगर कथन 'if (cell == nil) {' ... भी हटाया जाना चाहिए। यह एक त्रुटि देता है क्योंकि सेल वैकल्पिक प्रकार का नहीं है। –

2

तेज 3 संस्करण:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath!) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier:"CellIdentifier", for: indexPath) as UITableViewCell 
     return cell 
    } 
लोड हो जाए, आप एक सेल उदाहरण लाने के लिए निम्नलिखित का उपयोग कर सकते 0
1

स्विफ्ट 3 और स्विफ्ट में 4 संस्करण आप सिर्फ इस

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath as IndexPath) as UITableViewCell 
     cell.textLabel?.text = "cell number \(indexPath.row)." 

     //cell code here 

     return cell 
    } 

स्विफ्ट 2 में संस्करण

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

cell.textLabel?.text = "cell number \(indexPath.row)." 

     //cell code here 

     return cell 
    } 
संबंधित मुद्दे