2011-08-04 21 views
32

मैं ऑनलाइन सभी उदाहरणों के माध्यम से गया और यह पता नहीं लगा सका कि एनीमेशन के साथ टेबलव्यू में सेल को सही तरीके से कैसे जोड़ा जाए। आइए मान लें कि मेरे पास एक सेल वाला एक सेक्शन है और जब उपयोगकर्ता पहले सेल की एक्सेसरी पर क्लिक करता है तो मैं एक और सेल जोड़ना चाहता हूं।insertRowsAtIndexPaths का सही तरीके से उपयोग कैसे करें?

मेरे "जोड़ें" विधि से करता है:

- (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender { 
if (switch1.on) { 

    NSArray *appleComputers = [NSArray arrayWithObjects:@"WWWWW" ,@"XXXX", @"YYYY", @"ZZZZ", nil]; 
    NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:@"Computers"]; 
    [listOfItems replaceObjectAtIndex:0 withObject:appleComputersDict]; 
    [tblSimpleTable reloadData]; 

} 

है कौन सा काम कर रहा लेकिन कोई एनीमेशन है। मैं समझता हूं कि एनीमेशन जोड़ने के लिए, मुझे insertRowsAtIndexPaths का उपयोग करने की आवश्यकता है: RowAnimation के साथ, इसलिए मैंने कई विकल्पों को आजमाया लेकिन insertRowsAtIndexPaths को निष्पादित करते समय यह हमेशा क्रैश हो जाता है: RowAnimation विधि के साथ।

- (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender { 
if (switch1.on) { 

    NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0]; //ALSO TRIED WITH indexPathRow:0 
     NSArray *indexArray = [NSArray arrayWithObjects:path1,nil]; 
    [tblSimpleTable insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationRight]; 

} 
} 

क्या मैं गलत कर रहा हूँ:

मेरी हाल की कोशिश ऐसा करने से था? मैं इसे आसानी से कैसे कर सकता हूं? मैं इस पूरे इंडेक्सपैथफोररो चीज को नहीं समझता ... मैं यह भी नहीं समझता कि इस विधि के साथ मैं नए सेल में एक लेबल नाम जोड़ सकता हूं। कृपया मदद करें ... धन्यवाद !!

+0

क्या आपने 2 आइटम के साथ अपना डेटा स्रोत अपडेट किया था? –

उत्तर

22

का उत्पादन करेगा क्या सम्मिलित करने के लिए यह कह रहा है। यदि आप तालिकादृश्य में कोई पंक्ति जोड़ते हैं, तो सुनिश्चित करें कि बैकिंग डेटा पहले ही मेल खाने के लिए अपडेट हो चुका है।

+0

मेरे सूचकांक के बारे में क्या - क्या वे मानते हैं कि मैं एक पंक्ति पंक्ति में एक और पंक्ति जोड़ रहा हूं? – TommyG

+0

ठीक है, आप सही थे - मैंने अपनी नई तालिका/शब्दकोश के सभी अपडेट हटा दिए और अब यह काम करता है। धन्यवाद!! – TommyG

6

सबसे पहले, आपको अपडेट टेबल से पहले अपने डेटा मॉडल को अपडेट करना चाहिए। इसके अलावा, आप उपयोग कर सकते हैं:

[tableView beginUpdates]; 
// do all row insertion/delete here 
[tableView endUpdates]; 

और तालिका सभी एनीमेशन के साथ एक ही बार में बदल गया जब insertRowsAtIndexPaths है कि आपके UITableViewDataSource आवश्यकताओं से मिलान करने का उपयोग कर को ध्यान में रखना महत्वपूर्ण बात यह है (यदि आप इसे उल्लेख करें)

+0

ने कोशिश की - इससे कोई फर्क नहीं पड़ता। – TommyG

+0

मुझे लगता है कि आप परिवर्तन से पहले अपने डेटा मॉडल को अपडेट नहीं किया है। क्या आप अपवाद संदेश पोस्ट कर सकते हैं? –

+0

2011-08-04 10: 54: 55.601 व्यूटेस्ट [36300: बी 0 9 3] *** में सम्मिलन विफलता - [UITableView _endCellAnimationsWithContext:],/SourceCache/UIKit_Sim/UIKit-1448।89/UITableView.m: 995 2011-08-04 10: 54: 55.602 व्यूटेस्ट [36300: बी 0 9 3] कोरएनीमेशन: अपवाद को अनदेखा करना: अमान्य अद्यतन: धारा 0 में पंक्तियों की अवैध संख्या। मौजूदा खंड में मौजूद पंक्तियों की संख्या अद्यतन (1) अद्यतन (1) से पहले उस खंड में निहित पंक्तियों की संख्या के बराबर होना चाहिए, प्लस या उस खंड से डाली गई पंक्तियों की संख्या घटाएं (1 डाला गया, 0 हटाया गया)। – TommyG

22

यह प्रक्रिया दो चरणों में है:

सबसे पहले अपने डेटा स्रोत इस प्रकार अपडेट numberOfRowsInSection और cellForRowAtIndexPath अपनी पोस्ट-डालने डेटा के लिए सही मान वापस आ जाएगी। पंक्तियों को डालने या हटाने से पहले आपको यह करना होगा या आपको "पंक्तियों की अवैध संख्या" त्रुटि दिखाई देगी जो आपको मिल रही है। डालने या एक पंक्ति अपने डेटा स्रोत परिवर्तन नहीं करता है को हटाने

[tblSimpleTable beginUpdates]; 
[tblSimpleTable insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationRight]; 
[tblSimpleTable endUpdates]; 

सीधे शब्दों में;:

फिर अपने पंक्ति सम्मिलित आपको वह खुद करना है।

+0

+1 धन्यवाद, जिसने पूरी तरह मदद की! – TommyG

+0

यह पर्याप्त नहीं है: आपको डेटा स्रोत बदलने वाले कोड को startUpdates/endUpdates अनुभाग के अंदर भी रखना चाहिए, क्योंकि यह "लेनदेन" का भी हिस्सा है। – ettore

+0

आपको शुरुआत/अंत के अंदर अपना डेटासोर्स बदलना कोड नहीं रखना चाहिए क्योंकि यह तालिका अद्यतन से स्वतंत्र है। चिंताओ का विभाजन। "लेनदेन" सिर्फ तालिका प्रदर्शन अद्यतन है। –

0

तेज उपयोगकर्ताओं

// have inserted new item into data source 

// update 
self.tableView.beginUpdates() 
var ip = NSIndexPath(forRow:find(self.yourDataSource, theNewObject)!, inSection: 0) 
self.tableView.insertRowsAtIndexPaths([ip], withRowAnimation: UITableViewRowAnimation.Fade) 
self.tableView.endUpdates() 
+0

यहां स्विफ्ट ---> [मूल ट्रिलॉजी की ईमानदारी बनाए रखने के लिए UITableView अपडेट्स] में एक ट्यूटोरियल है (https: // grokswift। com/uitableview-update /), डेमो कैसे insertRowsAtIndexPaths और startUpdates का उपयोग करें –

2

insertRowsAtIndexPaths:withRowAnimation: और अपने डेटा मॉडल दोनों में परिवर्तन के लिए होने की जरूरत के बीचbeginUpdates और

endUpates मैं एक साधारण उदाहरण है कि काम करना चाहिए बना लिया है अपने दम पर। मैंने इसे समझने की कोशिश करने के चारों ओर एक सप्ताह बिताया क्योंकि मुझे कोई आसान उदाहरण नहीं मिला, इसलिए मुझे आशा है कि यह किसी के समय और सिरदर्द बचाएगा!

@interface MyTableViewController() 
@property (nonatomic, strong) NSMutableArray *expandableArray; 
@property (nonatomic, strong) NSMutableArray *indexPaths; 
@property (nonatomic, strong) UITableView *myTableView; 
@end 

@implementation MyTableViewController 

- (void)viewDidLoad 
{ 
    [self setupArray]; 
} 

- (void)setupArray 
{ 
    self.expandableArray = @[@"One", @"Two", @"Three", @"Four", @"Five"].mutableCopy; 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.expandableArray.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //here you should create a cell that displays information from self.expandableArray, and return it 
} 

//call this method if your button/cell/whatever is tapped 
- (void)didTapTriggerToChangeTableView 
{ 
    if (/*some condition occurs that makes you want to expand the tableView*/) { 
     [self expandArray] 
    }else if (/*some other condition occurs that makes you want to retract the tableView*/){ 
     [self retractArray] 
    } 
} 

//this example adds 1 item 
- (void)expandArray 
{ 
    //create an array of indexPaths 
    self.indexPaths = [[NSMutableArray alloc] init]; 
    for (int i = theFirstIndexWhereYouWantToInsertYourAdditionalCells; i < theTotalNumberOfAdditionalCellsToInsert + theFirstIndexWhereYouWantToInsertYourAdditionalCells; i++) { 
     [self.indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
    } 

    //modify your array AND call insertRowsAtIndexPaths:withRowAnimation: INBETWEEN beginUpdates and endUpdates 
    [self.myTableView beginUpdates]; 
    //HERE IS WHERE YOU NEED TO ALTER self.expandableArray to have the additional/new data values, eg: 
    [self.expandableArray addObject:@"Six"]; 
    [self.myTableView insertRowsAtIndexPaths:self.indexPaths withRowAnimation:(UITableViewRowAnimationFade)]; //or a rowAnimation of your choice 

    [self.myTableView endUpdates]; 
} 

//this example removes all but the first 3 items 
- (void)retractArray 
{ 
    NSRange range; 
    range.location = 3; 
    range.length = self.expandableArray.count - 3; 

    //modify your array AND call insertRowsAtIndexPaths:withRowAnimation: INBETWEEN beginUpdates and endUpdates 
    [self.myTableView beginUpdates]; 
    [self.expandableArray removeObjectsInRange:range]; 
    [self.myTableView deleteRowsAtIndexPaths:self.indexPaths withRowAnimation:UITableViewRowAnimationFade]; //or a rowAnimation of your choice 
    [self.myTableView endUpdates]; 
} 

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