2013-03-11 17 views
10

मैं इस कोड का उपयोग मेरी UITableViewCells के विशिष्ट पंक्तियों पर एक 'ताला' आइकन प्रदर्शित करने के लिए कोशिश कर रहा हूँ:UITableView सेल accessoryView छवि मुद्दा

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
      cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
      [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 

    } 

    return cell; 
} 

मैं एक अजीब परिणाम हो रही है - ताला शुरू में पंक्तियों 5 पर दिखाया जाता है , 9 लेकिन जब मैं नीचे स्क्रॉल करता हूं और सूची में आइकन को अन्य कोशिकाओं के एक्सेसरीव पर यादृच्छिक रूप से फिर से चलाया जाता है (वहां केवल 1 अनुभाग बीटीडब्लू) होता है, और स्क्रॉलिंग काफी झटकेदार और लगी हो जाती है ... जितना अधिक मैं ऊपर/नीचे स्क्रॉल करता हूं इसके अधिक उदाहरण प्रदर्शित होते हैं! क्यों? यहाँ बग कहाँ है?

सहायता, धन्यवाद!

+0

क्योंकि सेल अन्य पंक्तियों के लिए पुन: उपयोग किया गया है जब आप स्क्रॉल, मुझे लगता है कि आप पंक्ति 5 और 9 –

+0

के लिए एक अलग CellReuseIdentifier का उपयोग करना चाहिए मुझे लगता है कि कैसे करते हो? – mindbomb

उत्तर

21

कोशिकाओं का पुन: उपयोग किया जाता है। आपको प्रत्येक बार accessoryView रीसेट करने की आवश्यकता है। इसका सिर्फ एक छोटा सा परिवर्तन:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]]; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 
    } else { 
     cell.accessoryView = nil; 
    } 

    return cell; 
} 

बस पूरा करने के लिए, आप भी एरिक के समाधान इस तरह उपयोग कर सकते हैं - यह तेजी से हो सकता है के बाद से imageView हर बार नहीं बनाई गई है। लेकिन यह केवल एक न्यूनतम अंतर है। शायद आपके झंडे के अन्य कारण हैं।

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = nil; 
    if(indexPath.row == 5 || indexPath.row == 9) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"]; 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 
    } else { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 
    } 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    return cell; 
} 
+0

ठीक है, धन्यवाद! मैं इसे बाद में देखूंगा। लेकिन असल में मुझे उन कक्षों की आवश्यकता है जिनके पास 'प्रकटीकरण' ग्रे दाएं कोण आइकन – mindbomb

+0

हां 5 और 9 दिखाने के लिए कोई छवि नहीं है, फिर भी छवि दिखाएगी। दूसरों को नहीं। कर के देखो। – calimarkus

+0

और uitableview दस्तावेज़ पढ़ें - यह पुन: उपयोग करने की व्याख्या करता है। – calimarkus

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 
    if(indexPath.row == 5 || indexPath.row == 9) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"]; 
    } else { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 
    } 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 

    } 

    return cell; 
} 
+0

क्षमा करें, सही नहीं है, jaydee3 का समाधान बहुत आसान है .. धन्यवाद! – mindbomb

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