2011-04-07 21 views
7

में कई कस्टम सेल जोड़ना हालांकि यह सबसे अधिक पूछे जाने वाले प्रश्न में से एक है लेकिन मुझे एक व्यापक उत्तर नहीं मिला। मुझे UITableView में कस्टम सेल्स की आवश्यकता है। कुछ लेबल या टेक्स्ट फ़ील्ड और कुछ छवियों और बटन के साथ। मैंने प्रत्येक प्रकार के सेल के लिए अलग-अलग कक्षाएं बनाई हैं। मैं कई वर्गों के साथ समूह स्टाइल तालिका का उपयोग कर रहा हूं। अभी मैं खंड में अनुभाग के लिए और अगर-बाकी स्विच मामले से cellForIndexPath में कोशिकाओं द्वारा जोड़ा जा रहा पंक्तियों के लिए:UITableView

id cell; 
switch(indexPath.section) { 
    case 0: 
      if(indexPath.row==0) { 
       CellA *cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease]; 
       //configure cell 
       return cell; 
      } 
      else if(indexPath.row==1) { 
       CellB *cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease]; 
       //configure cell 
       return cell; 
      } 
      break; 
    case 1: 
      if(indexPath.row==0) { 
       CellC *cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease]; 
       //configure cell 
       return cell; 
      } 
      break; 
    default: 
      break; 
} 
return cell; 

मैं कोड ब्लॉक के अंदर कोशिकाओं की परिभाषा के अंत में सेल लौटने के साथ ही क्योंकि कारण के लिए है, सेल अपरिचित हो जाता है। इसे हल करने के लिए, मैंने शीर्ष पर आईडी के साथ सेल घोषित किया। लेकिन मुझे पता है कि यह सही तरीका नहीं है। मैं इस घोषणा को कैसे हल कर सकता हूं और कई प्रकार की कोशिकाओं के मुद्दे तक पहुंच सकता हूं?

इस समय 4-5 पंक्तियां हैं जो एक स्क्रीन फिट होती हैं और स्क्रॉलिंग की आवश्यकता नहीं होती है। तो, मैं कोशिकाओं का पुन: उपयोग नहीं कर रहा हूं। लेकिन संपादन करते समय अधिक पंक्तियां निचोड़ जाएंगी। और एक और टेबल में, स्क्रीन की स्क्रॉल कर सकते हैं और अधिक पंक्तियां हैं। इसका मतलब है कि मुझे कोशिकाओं का पुन: उपयोग करना होगा। तो, मेरे प्रश्न का दूसरा भाग है; मैं कई कस्टम कोशिकाओं का पुन: उपयोग कैसे कर सकता हूं?

+0

आप वास्तव में स्विच मामले – Markinson

उत्तर

11

अपने पहले प्रश्न का उत्तर देने के लिए, आप nil लौट सकते हैं क्योंकि आपके पास वापस जाने के लिए कोई अच्छा मूल्य नहीं है। यदि यह कभी भी इस मामले को हिट करता है, तो एक अपवाद फेंक दिया जाएगा; जैसा कि अब है, यह आपको फ्रेमवर्क कोड के अंदर कहीं भी EXC_BAD_ACCESS देने की संभावना है।

अपने दूसरे प्रश्न का उत्तर देने के लिए, प्रत्येक प्रकार के सेल का एक अनूठा पुन: उपयोग होना चाहिए। उदाहरण के लिए, सभी सेलए के पास @ "सेलए" का पुन: उपयोग करने वाला संकेतक हो सकता है। फिर आप उन्हें ठीक तरह से पुन: उपयोग करेंगे जैसा कि आप इस मामले में करेंगे कि सभी कक्ष समान थे: जब आपको सेलए कॉल [tableView dequeueReusableCellWithIdentifier:@"CellA"] की आवश्यकता होती है, जब आपको सेलब कॉल [tableView dequeueReusableCellWithIdentifier:@"CellB"] की आवश्यकता होती है, और इसी तरह। उदाहरण के लिए,

case 0: 
     if (indexPath.row == 0) { 
      CellA *cell = [tableView dequeueReusableCellWithIdentifier:@"CellA"]; 
      if (!cell) { 
       cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellA"] autorelease]; 
      } 
      // configure cell 
      return cell; 
     } 
     else if (indexPath.row == 1) { 
      CellB *cell = [tableView dequeueReusableCellWithIdentifier:@"CellB"]; 
      if (!cell) { 
       cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellB"] autorelease]; 
      } 
      // configure cell 
      return cell; 
     } 
     break; 
    case 1: 
     if (indexPath.row == 0) { 
      CellC *cell = [tableView dequeueReusableCellWithIdentifier:@"CellC"]; 
      if (!cell) { 
       cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellC"] autorelease]; 
      } 
      // configure cell 
      return cell; 
     } 
     break; 
+0

की जरूरत नहीं है प्रतिक्रिया के लिए धन्यवाद। सुरक्षित पक्ष पर रहने के लिए, मैं डिफ़ॉल्ट स्थिति के लिए एक सेल को परिभाषित कर रहा हूं, आशा है कि किसी भी मामले को छोड़कर डिफ़ॉल्ट रूप से संभाला जा सकता है और यह अंतिम रिटर्न स्टेटमेंट नहीं करेगा। – WaJiyaz

0

UITableView करने के लिए UIView.Add कस्टम कोशिकाओं, सहयोगी कस्टम सेल वर्गों जोड़े और प्रतिनिधियों -UITableviewDelegate और UITableViewDataSource लागू।

मामले 1: tableview पर दो कस्टम कोशिकाओं

समारोह tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell: CustomCell! 

    if indexPath.row == 0{ 
    cell = tableView.dequeueReusableCellWithIdentifier("Cell1ID", forIndexPath: indexPath) as CustomCell 
    //set cell2 
    } 
    if indexPath.row >= 1{ 
    cell = tableView.dequeueReusableCellWithIdentifier("Cell2ID", forIndexPath: indexPath) as CustomCell 
    let cons = aArray[indexPath.row - 1] 
    // set cell2 
    } 
    return cell 
} 

मामले 2: कस्टम कोशिकाओं के वैकल्पिक प्रदर्शन (यानी uisegmentcontrol का उपयोग)

var CellIdentifier: String = "Cell1ID" 

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

//additional code 
return cell 

} 
else { 
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! SecondTableViewReportsCell 

//Additional code 

return cell 
} 
} 

मामले 3: वैकल्पिक कस्टम कोशिकाओं (यानी, वे भी अजीब)

var CellIdentifier: String = "Cell1ID" 

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

var cell: CustomCell! 

if (indexPath.row % 2 == 0) { 

let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! FirstTableViewCell 

} 
else 
{ 
     let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! SecondTableViewCell 
} 
return cell 
}