2015-10-24 6 views
7

[UITableViewCell] < - [genericCell] < - [CELL1], [CELL2], [CELL3]स्विफ्ट। UITableViewCell पदानुक्रम के समुचित प्रारंभ

नमस्कार। कृपया उपरोक्त पदानुक्रम की कल्पना करें। मेरे कोड में मेरे पास ऑब्जेक्ट्स बिल्कुल genericCell टाइप नहीं हैं, लेकिन यह वर्ग कुछ गुण साझा करता है।

मेरे कोड में इनसाइट का कौन सा डिज़ाइन होना चाहिए? Cell1 के बारे में

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 
    //my stuff (initializing shared properties) 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 

} 

लेकिन क्या: मैं genericCell के लिए संरचना निम्नलिखित है? Cell1 इंस्टेंस के प्रारंभ के माध्यम से "मेरी सामग्री" संचालन के लिए genericCell में init(style: UITableViewCellStyle, reuseIdentifier: String?) का उपयोग कैसे कर सकता हूं? अब वे प्रदर्शन नहीं करते हैं।


संपादित

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let typeOfCell = FbDataManager.sharedInstance.posts[indexPath.row][FbDataManager.sharedInstance.typeParameter] as! String 

     switch typeOfCell { 
      case self.linkTypeOfPost: 

       var cell = tableView.dequeueReusableCellWithIdentifier(self.linkCellIdentifier) as? FbLinkPostViewCell 
       if cell == nil { 
        cell = FbLinkPostViewCell.init(style: .Default, reuseIdentifier: self.linkCellIdentifier) 
       } 
//... 

हाय फिर से। यह टेबल व्यू के प्रतिनिधि से हिस्सा है, बीटीडब्ल्यू मैंने अपने कोड में अभिनव के इनिट्स की प्रतिलिपि बनाई है और फिर वे लोग काम नहीं कर रहे हैं। (कंसोल के लिए कोई आउटपुट नहीं)

उत्तर

11

मुझे यकीन नहीं है कि मैं आपके प्रश्न को सही ढंग से समझता हूं, लेकिन यह वर्गों के बीच विरासत के बारे में प्रतीत होता है। तो मूल रूप से आपके पास "जेनेरिकसेल" श्रेणी है जो "UITableViewCell", और "CellOne", "CellTwo" और "CellThree" कक्षाओं से प्राप्त होती है जो प्रत्येक "जेनेरिकसेल" से प्राप्त होती हैं। आप शैली साथ init के माध्यम से जाना चाहते हैं, एक तरह से इसे सेट अप करने के लिए इस तरह होगा:

class GenericCell: UITableViewCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     // code common to all your cells goes here 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 
} 

class CellOne: GenericCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) // the common code is executed in this super call 
     // code unique to CellOne goes here 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 
} 

फिर आप तो जैसे अपनी मेज दृश्य के डेटा स्रोत में CellOne के उदाहरण बना सकते हैं:

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

    var cell = tableView.dequeueReusableCellWithIdentifier("cell") 
    if (cell == nil) { 
     cell = CellOne.init(style: .Default, reuseIdentifier: "cell") 
    } 
    return cell! 
} 

प्रत्येक उदाहरण के लिए यह अब "जेनेरिकसेल" में किए गए सामान्य सेटअप के माध्यम से और फिर "सेलऑन" में अद्वितीय सेटअप के माध्यम से जाएगा। "सेलटवो" और "सेल थ्री" तदनुसार स्थापित किए जाएंगे।

संपादित

एक सभी तीन सेल प्रकार के उदाहरणों को कॉन्फ़िगर कैसे करें के और अधिक ठोस उदाहरण:

:

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

     // you need to write a method like this to figure out which type you need: 
     let cellID = self.cellIDForIndexPath(indexPath) // returns either "cell1", "cell2" or "cell3" 

     // dequeue or init a cell of the approriate type 
     var cell = tableView.dequeueReusableCellWithIdentifier(cellID) 
     if (cell == nil) { 
      switch cellID { 
       case "cell1": cell = CellOne.init(style: .Default, reuseIdentifier: "cell") 
       case "cell2": cell = CellTwo.init(style: .Default, reuseIdentifier: "cell") 
       case "cell3": cell = CellThree.init(style: .Default, reuseIdentifier: "cell") 
       default: cell = UITableViewCell() 
      } 

     } 

     // configure the individual cell if needed (you need to implement methods + logic here that fit your data) 
     (cell as! GenericCell).configureForData(self.dataForIndexPath(indexPath)) 

     return cell! 
    } 
+0

धन्यवाद! मेरे पास आपके कोड में जैसे ही लिखा है, तो 'if' ब्लॉक नहीं है। तो क्या 'dequeueReusableCellWithIdentifier' 'if'' शाखा में 'init' की तरह आमंत्रित करता है? – drewpts

+1

नहीं। पुन: प्रयोज्य कोशिकाओं का विचार यह है कि वे केवल एक बार शुरू किए जाते हैं और फिर पुन: उपयोग किए जाते हैं। तो 'init' केवल तभी बुलाया जाता है जब सेल पहले शुरू नहीं किया गया था (यानी अगर हम 'if' की सामग्री को दबाते हैं)। आपको पहचानकर्ता के साथ अपने सेल वर्गों को अलग करना होगा: जांचें कि आपको किस श्रेणी के लिए इंडेक्स की आवश्यकता है, फिर जांचें ("सेल 1", "सेल 2" और "सेल 3" जैसे अद्वितीय पहचानकर्ता के साथ) यदि वह विशेष प्रकार 'deqeue' हो सकता है डी या शुरू करने की जरूरत है। यदि एक ही कक्षा के कई सेल्स हैं (उदा। 3 "सेलऑन") जिन्हें अलग-अलग कॉन्फ़िगर करने की आवश्यकता है, तो मैं इसके लिए एक नई विधि तैयार करूंगा। – Gamma

+0

उत्तर में कुछ कोड जोड़ा गया है जो आशा करता है कि यह दृष्टिकोण आपके सेटअप के साथ काम करेगा। – Gamma

2

यह मैं कैसे पदानुक्रम आप ने उल्लेख किया लेट होता है चरण 1: जेनेरिक सेल क्लास

बनाएं 210

चरण 2: विशेष सेल 1 वर्ग बनाओ:

class MyCell1 : GenericCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     print("MyCell1 Initialization Done") 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 
} 

चरण 3:

class MyCell2 : GenericCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     print("MyCell2 Initialization Done") 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 
} 

चरण 4:: विशेष सेल 3 वर्ग बनाने विशेष सेल 2 वर्ग बनाओ :

class MyCell3 : GenericCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     print("MyCell3 Initialization Done") 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 
} 

चरण 5: अंत में इस तरह की कोशिकाओं का उपयोग करें:

let cell1 = MyCell1.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell1") 
let cell2 = MyCell2.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell2") 
let cell3 = MyCell3.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell3") 

पुनश्च: यह विशिष्ट कोशिकाओं में रूप में अच्छी तरह सामान्य कक्ष में गुण स्थापित करने की गारंटी होगी।

संपादित करें: इस तरह आप cellForRowAtIndexPath में कोशिकाओं का प्रयोग करेंगे है:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 
     let cell1 = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as MyCell1 

     if cell1 == nil { 
      cell1 = MyCell1.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell1") 
     } 

     // Do your cell property setting 

     return cell1 
    } else if indexPath.section == 1 { 
     let cell2 = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as MyCell2 

     if cell2 == nil { 
      cell2 = MyCell2.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell2") 
     } 

     // Do your cell property setting 

     return cell2 
    } else { 
     let cell3 = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as MyCell3 

     if cell3 == nil { 
      cell3 = MyCell3.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell3") 
     } 

     // Do your cell property setting 

     return cell3 
    } 
} 
+0

धन्यवाद। हां, मेरे पास कोड जैसा आपने लिखा है। ऐसा लगता है कि यह "dequeueReusableCellWithIdentifier" विधि के आसपास कुछ है। कृपया अगले उत्तर पर मेरी टिप्पणी देखें। – drewpts

+1

मैंने अपना जवाब अपडेट करने के लिए अपडेट किया है कि इसे 'cellForRowAtIndexPath:' पर ​​कैसे देखना चाहिए। जब 'UITableView' इंस्टेंस' dequeueReusableTileWithIdentifier: 'के लिए कॉल करता है, तो सेल को पुन: प्रारंभ नहीं किया जाता है। इसके बजाए, उस कॉल में, 'UITableViewCell' जिसे अस्वीकार कर दिया गया है उसे' readyForReuse' 'कॉल किया जाएगा। – Abhinav

+0

मैं उन inizializations में एक कस्टम पैरामीटर कैसे जोड़ सकते हैं? – dpstart