2008-12-01 5 views
6

पर काम नहीं कर रहा है और उसे हटा रहा है, मुझे उसी UITableView से UITableViewCells को डालने और हटाने में काफी दर्द हो रहा है!UITableViewCell को एक ही समय में

मैं सामान्य रूप से कोड पोस्ट नहीं है, लेकिन मुझे लगा कि यह दिखा, जहां मैं समस्या आ रही है का सबसे अच्छा तरीका था:


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 5; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    if (iSelectedSection == section) return 5; 
    return 1; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    //NSLog(@"drawing row:%d section:%d", [indexPath row], [indexPath section]); 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if (iSelectedSection == [indexPath section]) { 
     cell.textColor = [UIColor redColor]; 
    } else { 
     cell.textColor = [UIColor blackColor]; 
    } 


    cell.text = [NSString stringWithFormat:@"Section: %d Row: %d", [indexPath section], [indexPath row]]; 

    // Set up the cell 
    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Navigation logic -- create and push a new view controller 

    if ([indexPath row] == 0) { 

     NSMutableArray *rowsToRemove = [NSMutableArray array]; 
     NSMutableArray *rowsToAdd = [NSMutableArray array]; 

     for(int i=0; i<5; i++) { 

      //NSLog(@"Adding row:%d section:%d ", i, [indexPath section]); 
      //NSLog(@"Removing row:%d section:%d ", i, iSelectedSection); 

      [rowsToAdd addObject:[NSIndexPath indexPathForRow:i inSection:[indexPath section]]]; 
      [rowsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:iSelectedSection]]; 

     } 

     iSelectedSection = [indexPath section]; 

     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:rowsToRemove withRowAnimation:YES]; 
     [tableView insertRowsAtIndexPaths:rowsToAdd withRowAnimation:YES]; 
     [tableView endUpdates]; 

    } 
} 

इस कोड को 5 वर्गों बनाता है, पहली पंक्ति (0 से अनुक्रमित) 5 पंक्तियों के साथ। जब आप कोई सेक्शन चुनते हैं - यह उस अनुभाग से पंक्तियों को हटा देता है जिसे आपने पहले चुना था और आपके द्वारा चुने गए अनुभाग में पंक्तियां जोड़ती हैं।

http://www.freeimagehosting.net/uploads/1b9f2d57e7.png http://www.freeimagehosting.net/uploads/1b9f2d57e7.png

छवि यहाँ:

Pictorally, जब मैं एप्लिकेशन को लोड, मैं इस तरह कुछ है http://www.freeimagehosting.net/uploads/1b9f2d57e7.png

धारा 2 के एक तालिका पंक्ति 0 चयन करने के बाद, मैं फिर हटाएं खंड 1 (डिफ़ॉल्ट रूप से चयनित किया जाता है) की पंक्तियों और खंड 2. की पंक्तियों को जोड़ने लेकिन मैं इस मिल:

http://www.freeimagehosting.net/uploads/6d5d904e84.png http://www.freeimagehosting.net/uploads/6d5d904e84.png

छवि यहां: http://www.freeimagehosting.net/uploads/6d5d904e84.png

... जो मैं होने की उम्मीद नहीं करता! ऐसा लगता है कि सेक्शन 2 की पहली पंक्ति किसी भी तरह बनी हुई है - भले ही यह निश्चित रूप से हटा दी जाए।

यदि मैं सिर्फ एक [tableView reloadData] करता हूं, तो सब कुछ सामान्य के रूप में दिखाई देता है ... लेकिन मैं स्पष्ट रूप से अच्छे एनिमेशन की कल्पना करता हूं।

अगर कोई यहां कुछ प्रकाश डाल सकता है तो मैं वास्तव में इसकी सराहना करता हूं! यह मुझे थोड़ा पागल कर रहा है!

धन्यवाद, निक।

उत्तर

0

FYI: यह बग 2.2 आईफोन अपडेट के साथ पूरी तरह से तय किया गया प्रतीत होता है। धन्यवाद ऐप्पल! निक।

0

आपके द्वारा पोस्ट किए गए कोड में, आपका लूप इंडेक्स 0 से 4 तक चलता है, जो बताता है कि यह अनुभाग 0 में पंक्तियों के सभी को हटा देगा, और फिर अनुभाग 2 में पांच नई पंक्तियां जोड़ देगा। चूंकि प्रत्येक अनुभाग में पहले से ही है एक पंक्ति 0, यह तालिका में दूसरा उदाहरण 2, पंक्ति 0 का उदाहरण जोड़ देगा।

मैंने 4 1 से अपने पाश रन होने सुझाव है:

for (int i=1; i<5; i++) 
{ 
    // ... 
}
+0

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

1

मुझे लगता है कि numberOfRowsInSection याद करने लगते हैं: कहा जाता है प्राप्त करेंगे जब आप deleteRows फोन या insertRow, तुम सच में सावधान रहने की जरूरत है कि वास्तविकता numberOfRowsInSection Cliams आपके परिवर्तन से मेल खाता है। इस मामले में आप iSelectedSection = [indexPath अनुभाग] को स्थानांतरित करने का प्रयास करना चाहेंगे; अंत अद्यतन के बाद लाइन।

1

मुझे याद नहीं है कि मैंने इसे कहाँ पढ़ा है, लेकिन मुझे विश्वास है कि आपको तालिका दृश्य प्रतिनिधि कार्यों में से किसी एक के अंदर से तालिका पंक्ति अपडेट (सम्मिलन और हटाना) नहीं करना चाहिए। मुझे लगता है कि ऑब्जेक्ट के रूप में अपडेट करने के लिए जरूरी आवश्यक जानकारी के साथ निष्पादित चयनकर्ताऑनमेन थ्रेड करना बेहतर विकल्प होगा।कुछ ऐसा:

- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // .... 
    [self performSelectorOnMainThread: @selector(insertRows:) 
          withObject: someObjectOrNil]; // double check args 
} 

- (void) insertRows: (NSObject*)someObjectOrNil { 
    [tableView beginUpdates]; 
    // update logic 
    [tableView endUpdates]; 

    // don't call reloadData here, but ensure that data returned from the 
    // table view delegate functions are in sync 
} 
6

इसे काम करने के लिए संघर्ष किया गया। मेरी तालिका में एक पंक्ति जोड़ने के लिए मेरा कोड यहां देखें:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[tableView beginUpdates]; 
[dataSource insertObject:[artistField text] atIndex:0]; 
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; 
[tableView endUpdates]; 

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

  • कोई संबंधित समस्या नहीं^_^