2010-02-28 22 views
5

मुझे एक कस्टम UITableView अनुभाग शीर्षलेख को एनिमेट करने में समस्या हो रही है।
लक्ष्य ढहने योग्य वर्ग बनाना था।
जब मैं कस्टम हेडर पर टैप करता हूं तो पहली बार यह अपेक्षित के रूप में एनिमेट करता है, हालांकि हर बार जब यह मूल स्थान में डुप्लिकेट छोड़ देता है और दूसरे को एनिमेट करता है।UITableView कस्टम सेक्शन हैडर, डुप्लिकेट समस्या

छवि उदाहरण:

alt text http://img35.imageshack.us/img35/4018/screenshot2010022722565.png alt text http://img291.imageshack.us/img291/8287/screenshot2010022723035.png
मेरा कस्टम हेडर:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
     UIView* customView = [[[UIView alloc]initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]autorelease]; 
     customView.backgroundColor = [UIColor lightGrayColor]; 

     UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
      headerLabel.backgroundColor = [UIColor clearColor]; 
      headerLabel.opaque = NO; 
      headerLabel.textColor = [UIColor darkGrayColor]; 
      headerLabel.font = [UIFont boldSystemFontOfSize:16]; 
      headerLabel.frame = CGRectMake(10, 7, 260.0, 44.0); 
      headerLabel.textAlignment = UITextAlignmentCenter; 
      NSDictionary *dictionary = [self.data objectAtIndex:section]; 
      headerLabel.text = [dictionary objectForKey:@"Title"]; 

      [customView addSubview:headerLabel]; 


      // add button to right corner of section 
     UIButton* headerButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 320, 44)]; 
      headerButton.center = CGPointMake(160.0, 22.0); 
      headerButton.backgroundColor = [UIColor clearColor]; 
      headerButton.tag = section; 
      [headerButton addTarget:self action:@selector(expandSection:) forControlEvents:UIControlEventTouchUpInside]; 

      [customView addSubview:headerButton]; 

      return customView; 
} 

मेरे एनीमेशन विधि:

- (void) expandSection:(id)sender { 

    if (expandedSection == [sender tag]) { 
     expandedSection = -1; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
    }else if (expandedSection == -1){ 
     expandedSection = [sender tag]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
    }else{ 
     [self.tableView beginUpdates]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone]; 
     expandedSection = [sender tag]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableView endUpdates]; 

    } 
    //[self.tableView reloadData]; 
} 

मैं क्या चल रहा बिल्कुल यकीन नहीं है, लेकिन उदाहरण बताते हैं कि मुझे कुछ सौदा करने की ज़रूरत है। मैंने कुछ चीजों की कोशिश की है लेकिन मैं इसे समझ नहीं सकता। इस पर कोई भी मदद महान होगा!

संपादित करें: मुझे विश्वास है कि समस्या यह है कि पुनः लोड करना कस्टम दृश्य को उदाहरण के कारण बना रहा है। मैं दृश्य को जारी नहीं कर सकता क्योंकि मुझे एनीमेशन के लिए अद्यतन करने के संदर्भ के रूप में इसकी आवश्यकता है। इस पर कोई विचार है कि मैं इसे ठीक करने के लिए क्या कर सकता हूं?

उत्तर

8

समाधान मिला।

तालिका को प्रत्येक परिवर्तन से पहले पुनः लोड करने की आवश्यकता है। इस तरह से कोई भी बदलाव करने से पहले तालिका नवीनतम स्थिति में है।

[self.tableView reloadData] जोड़ें; "विस्तार जांच" विधि में मुट्ठी प्रविष्टि के रूप में ।

कोड:

- (void) expandSection:(id)sender { 

    [self.tableView reloadData]; 

    if (expandedSection == [sender tag]) { 
     expandedSection = -1; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
    }else if (expandedSection == -1){ 
     expandedSection = [sender tag]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
    }else{ 
     [self.tableView beginUpdates]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone]; 
     expandedSection = [sender tag]; 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableView endUpdates]; 

    } 
    //[self.tableView reloadData]; 
} 
+0

धन्यवाद, यह एक संबंधित समस्या को हल करता है जहां मैं एक ही पंक्तियों को हटाने/डालने का एनिमेटेड प्रभाव उत्पन्न करना चाहता था, जहां पहले बिना 'रीलोडडाटा' को कॉल किए बिना, यह केवल हटाए गए संदेशों को एनिमेट करेगा, लेकिन आवेषण को एनिमेट नहीं करेगा। यह बहुत अजीब मुद्दा था। – Jeremy

+0

अच्छा तरीका ... + 1 आपके लिए आदमी – Sabby

2

मैं ने वही समस्या है जो गतिशील ऊंचाई कोशिकाओं के इस्तेमाल से की वजह से किया गया था। मेरे पास एक विस्तारणीय कस्टम हेडर व्यू था और जब मैं तालिका को अद्यतन कर रहा था, अनुभाग के संबंधित पंक्तियों को डालने और निकालने के लिए दृश्य (जिसका अर्थ है कि वे विस्तार कर रहे थे, क्रमशः ध्वस्त हो रहे थे), अनुभाग हेडर जो UITableViewHeaderFooterView का उप-वर्ग था, पुनर्नवीनीकरण नहीं किया गया था। तो मूल रूप से एक नया आवंटित किया गया था और पुराने पर जोड़ा गया जिसके परिणामस्वरूप ओवरलैपिंग दृश्य थे। सेल पहचानकर्ता ठीक से सेट किया गया था, इसलिए कुछ और होना चाहिए था। जब मैंने tableView.sectionHeaderHeight = UITableViewAutomaticDimension हटा दिया और func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat लागू किया, तो दृश्य को ठीक से पुनर्नवीनीकरण किया गया और प्रत्येक अनुभाग के लिए केवल एक हेडर व्यू प्रदर्शित किया गया।

एक अन्य समाधान है कि मैं वास्तव काम करने के लिए निकला UITableViewHeaderFooterView के बजाय UITableViewCell का इस्तेमाल किया गया है और जब आप func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? में वापसी तुम सिर्फ return cell.contenView, इस काम करेंगे क्योंकि विधि एक UIView वापस जाने के लिए की आवश्यकता है और के बाद से UITableViewCell की contentView एक है UIView यह ठीक काम करता है। UITableViewCell के माध्यम से UITableView के रीसाइक्लिंग तंत्र का उपयोग करना है और इसे कॉन्फ़िगर करने के बाद बस अपनी सामग्री को वापस करना है।

निष्कर्ष। UITableViewHeaderFooterView के कारण होने वाली समस्या को स्वयं आकार देने वाली तालिका के साथ प्रयोग किया जाता है जब कोशिकाओं की ऊंचाई की गणना करने के बजाय कक्ष और UITableViewAutomaticDimension देखें।

+0

मजेदार बात यह है कि मैं हेडर के लिए पहले से ही UITableViewCells का उपयोग कर रहा था, स्वयं आकार देने वाली कोशिकाओं का उपयोग नहीं कर रहा था, और फिर भी मुझे समस्या थी :) – Gobe