2011-12-02 13 views
5

यह मेरा cellForRowAtIndexPath UITableView प्रतिनिधि विधि के लिए संक्षिप्त कोड है:UITableView indexPath खंड तर्क

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
cell.textLabel.text = @""; 
cell.detailTextLabel.text = @""; 
cell.backgroundView = NULL; //problem here 

// Initialize cell 
//blah blah blah 
//now to the good part... 

if(indexPath.section == 1) { 
    cell.backgroundView = deleteButton; 
    cell.userInteractionEnabled = YES; 
    cell.textLabel.text = nil; 
    cell.detailTextLabel.text = nil; 
} 

else if(indexPath.section == 0) { 
    NSLog(@"section: %i row: %i", indexPath.section, indexPath.row); 
    switch (indexPath.row) { 
     case 0: 
      cell.textLabel.text = @"foobar"; 
      //more stuff 
      break; 

     //lots more cases 

     default: 
      break; 
    } 
} 

return cell; 

}

मेरे समस्या यह है कि में खंड 1 (अनुभाग 0 प्रथम कक्ष 10 कोशिकाओं, खंड 1 है केवल 1 सेल है) जानकारी को असाइन किया जा रहा है जिसे केवल पहले खंड के सेल 0 को असाइन किया जाना चाहिए। तो, हटाएं बटन पृष्ठभूमि और आदि प्राप्त करने के बजाय, इसे लेबल शीर्षक "foobar" मिलता है। मुझे सच में यकीन नहीं है कि यह क्यों हो रहा है, क्योंकि मेरे अगर कथन बहुत स्पष्ट हैं। कोई विचार?

संपादित करें: पृष्ठभूमि को सेट करने से NULL को देखने के लिए उन कक्षों को टेक्स्ट के साथ, जब वे दृश्य छोड़ते हैं, किसी भी पृष्ठभूमि के बिना वापस आते हैं। तो यह एक व्यवहार्य समाधान नहीं है। साथ ही, विवरण टेक्स्ट टेक्स्ट के लिए पाठ अभी भी उस सेल पर सेट है जिसमें कोई टेक्स्ट नहीं होना चाहिए। के रूप में एलेक्स समझना (द्वारा सिफारिश की

enter image description here

समाधान,:

यह है कि यह कैसे सेल backgroundViews शून्य करने के लिए सेट और नष्ट सेल पर प्रदर्शित पाठ के साथ, लग रहा है वह जगह है जहाँ ऐसा नहीं होना चाहिए इस कोड के साथ) वर्ष विपंक्ति कोड की जगह:

NSString* identifier; 
if(indexPath.section == 0) 
    identifier = @"0"; 
else 
    identifier = @"1"; 
UISwitch *switchView; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease]; 

उत्तर

5

आप उन कक्षों का पुन: उपयोग के बारे में प्रलेखन पढ़ना चाहिए हालांकि कि indexPath के लिए अद्वितीय डेटा के किसी भी सेटिंग किया जाना चाहिए।

आपको दो खंडों में से प्रत्येक के लिए एक अलग पुन: उपयोगकर्ता का उपयोग करना चाहिए, क्योंकि वे मौलिक रूप से अलग-अलग स्टाइल कोशिकाएं हैं।

+0

आह, हाँ, उसने ऐसा किया। समाधान के साथ सवाल अद्यतन किया। –

0

कोड की इन पंक्तियों को एक बंद } कोष्ठक जोड़ें:

+०१२३५१६४१०६१
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
.... 
.... 
.... 
if(indexPath.section == 1) { 
    .... 
    .... 
} 
if(indexPath.section == 0) { 
    .... 
    .... 
} 

और मुझे संदेह है कि आपके टेबल में बेहतर परिणाम होंगे।

जिस तरह से चीजें अभी काम कर रही हैं (कम से कम जहां तक ​​मैं आपके कोड में बता सकता हूं), आप एक सेल बनाते हैं और इसे किसी चीज़ में प्रारंभ किया जाता है। यह किसी भी अन्य अनुरोध के मूल्य & मानों पर कभी भी रीसेट नहीं हो जाता है।

+0

ऐसा नहीं किया- अब दोनों अनुभागों में पंक्ति 0 पर दोनों कक्षों को अनुभाग 0 में कक्ष से पाठ और अनुभाग 1 में कक्ष से पृष्ठभूमि प्राप्त होता है। लेकिन जैसा मैंने पाया, दुर्घटना, dequeueReusableCellWithIndentifier और reuseIndentifier के लिए अलग पहचानकर्ताओं का उपयोग कर रहा है ... क्या यह एक बुरा अभ्यास है? –

+0

अपने मूल प्रश्न को अपने नए कोड के साथ अपडेट करें ... और हाँ, पहचानकर्ता एक तालिका दृश्य के भीतर समान होना चाहिए। –

+0

मैंने नए कोड –

0

आपका कोड गलत है।

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) 

dqueueReuseableCellWithIdentifier एक मौजूदा पूर्व में बनाए गए सेल वापस आ जाएगी अगर यह नहीं रह गया है उपयोग में। If cell == nil आपको एक नया सेल बनाना चाहिए और सभी कोशिकाओं के लिए डिफ़ॉल्ट डिफ़ॉल्ट सेट करना चाहिए। बाद

if(cell==nil) block. 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1   reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
[email protected]"unique text"; 
return cell; 
+0

कृपया मिशेल डॉटर्मन के उत्तर पर पोस्ट की गई टिप्पणी देखें। –

संबंधित मुद्दे