2012-04-04 53 views
0

在我的应用程序中,我使用NSNotificationCenter批量返回10个用户对象。当我得到10个对象时,我想在表格视图中插入这些数据。对于这个我使用下面的代码在uitableview中插入行时'NSInternalInconsistencyException'iphone

- (void)viewDidAppear:(BOOL)animated { 

    recordCounter = 0; 
    noOfRowsCounter = 0; 

    // Some code 
} 

- (void) gotNotification: (NSNotification *) notification { 

    NSMutableArray *tempArray = [[notification userInfo]valueForKey:KEY]; 

    NSLog(@"recordCounter BEFORE=> %d",recordCounter); 
    NSLog(@"noOfRowsCounter BEFORE=> %d",noOfRowsCounter); 

    self.insertIndexPaths = [[NSMutableArray alloc] init]; 
    for (User *user in tempArray) 
    { 
     [self.contactsArray addObject:user]; 

     recordCounter++; 
    } 

    for (noOfRowsCounter = noOfRowsCounter; noOfRowsCounter < recordCounter; noOfRowsCounter++) { 

     [self.insertIndexPaths addObject:[NSIndexPath indexPathForRow:recordCounter inSection:0]]; 

    } 

    NSLog(@"recordCounter AFTER=> %d",recordCounter); 
    NSLog(@"noOfRowsCounter AFTER=> %d",noOfRowsCounter); 
    NSLog(@"self.insertIndexPaths count=> %d",[self.insertIndexPaths count]); 
    [contactsTableView beginUpdates]; 
    [contactsTableView insertRowsAtIndexPaths:self.insertIndexPaths 
          withRowAnimation:UITableViewRowAnimationNone]; 
    [contactsTableView endUpdates]; 

    [self.insertIndexPaths removeAllObjects]; 
} 

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

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

    // I am doing this as I have 2 records in single row 

    if ([self.insertIndexPaths count]%2 == 0) { 

     NSLog(@"In IF::rows:: %d",[self.insertIndexPaths count]/2); 
     return [self.insertIndexPaths count]/2; 
    } 
    else { 
     NSLog(@"In ELSE::rows:: %d",[self.insertIndexPaths count]/2+1); 
     return [self.insertIndexPaths count]/2 + 1; 
    } 
} 

如果我跑我的应用程序,我得到以下错误&我的应用程序崩溃。这里是我的控制台日志

2012-04-04 12:15:36.559 AppName[1742:6803] recordCounter BEFORE=> 0 
2012-04-04 12:15:36.563 AppName[1742:6803] noOfRowsCounter BEFORE=> 0 
2012-04-04 12:15:38.539 AppName[1742:6803] recordCounter AFTER=> 10 
2012-04-04 12:15:38.543 AppName[1742:6803] noOfRowsCounter AFTER=> 10 
2012-04-04 12:15:38.546 AppName[1742:6803] self.insertIndexPaths count=> 10 

2012-04-04 12:15:42.971 AppName[1742:6803] In IF::rows:: 5 

2012-04-04 12:15:44.292 AppName[1742:6803] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1448.89/UITableView.m:995 
2012-04-04 12:15:44.306 AppName[1742:6803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (10 inserted, 0 deleted).' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x36c4164f __exceptionPreprocess + 114 
    1 libobjc.A.dylib      0x332fec5d objc_exception_throw + 24 
    2 CoreFoundation      0x36c41491 +[NSException raise:format:arguments:] + 68 
    3 Foundation       0x334ec573 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 62 
    4 UIKit        0x30ca4379 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 4500 
    5 UIKit        0x30cae2f9 -[UITableView endUpdatesWithContext:] + 28 
    6 UIKit        0x30cae2d5 -[UITableView endUpdates] + 16 
    7 AppName      0x00004927 -[AppNameViewController batchNotification:] + 770 
    8 Foundation       0x334a4183 _nsnote_callback + 142 
    9 CoreFoundation      0x36c1020f __CFXNotificationPost_old + 402 
    10 CoreFoundation      0x36baaeeb _CFXNotificationPostNotification + 118 
    11 Foundation       0x334a15d3 -[NSNotificationCenter postNotificationName:object:userInfo:] + 70 
    12 AppName      0x00007705 -[AddressBook getAddressBookData] + 1700 
    13 AppName      0x0000447b -[AppNameViewController retriveAddressBookData] + 102 
    14 Foundation       0x334b3389 -[NSThread main] + 44 
    15 Foundation       0x335255cd __NSThread__main__ + 972 
    16 libsystem_c.dylib     0x350c0311 _pthread_start + 248 
    17 libsystem_c.dylib     0x350c1bbc start_wqthread + 0 
) 
terminate called after throwing an instance of 'NSException' 

我想在表格视图中添加这些记录。我怎样才能做到这一点。由于

UPDATE:的cellForRowAtIndexPath准则

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     NSArray *cellArray=[[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil]; 
     //NSLog(@"cellArray count => %d",[cellArray count]); 
     cell=[cellArray objectAtIndex:0]; 
    } 

    // Configure the cell... 

    // For showing 2 records in single row 

    int row = indexPath.row * 2; 
    if (row <= [self.contactsArray count]) 
    { 
     User *user = [self.contactsArray objectAtIndex:row]; 
     NSLog(@"In ROW: %@",user.firstName); 
     cell.firstNameLabel.text=[NSString stringWithFormat:@"%@",nameString]; 
    } 

    if ((row + 1) < [self.contactsArray count]) 
    { 

     User *user = [self.contactsArray objectAtIndex:row+1]; 
     NSLog(@"In ROW+1: %@",user.firstName); 
     cell.secondNameLabel.text=[NSString stringWithFormat:@"%@",nameString]; 
    } 

    return cell; 
} 

回答

1

根据数据要在在部分行,但你的 self.insertIndexPaths的数目返回图5是具有10条索引路径

[contactsTableView insertRowsAtIndexPaths:self.insertIndexPaths 
         withRowAnimation:UITableViewRowAnimationNone]; 

这些行指定要插入的10行,如在错误明确提到在更新之后您获得的行数应该等于在部分中的行数中返回的值。

int presentrows = [yourtablename numberOfRowsInSection:0]; 
    int Finalnumberofrows; 
    if ([self.insertIndexPaths count]%2 == 0) { 
    Finalnumberofrows = [self.contactsArray count]/2; 
} 
else { 
    Finalnumberofrows = [self.contactsArray count]/2 + 1; 
} 
    for (presentrows; presentrows < Finalnumberofrows; presentrows++) { 

    [self.insertIndexPaths addObject:[NSIndexPath indexPathForRow:presentrows inSection:0]]; 

} 

更换线

for (noOfRowsCounter = noOfRowsCounter; noOfRowsCounter < recordCounter; noOfRowsCounter++) { 

     [self.insertIndexPaths addObject:[NSIndexPath indexPathForRow:recordCounter inSection:0]]; 

    } 

,并在索引方法 替换 表视图细胞用于行 - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分 {

// I am doing this as I have 2 records in single row 

if ([self.contactsArray count]%2 == 0) { 

    NSLog(@"In IF::rows:: %d",[self.insertIndexPaths count]/2); 
    return [self.contactsArray count]/2; 
} 
else { 
    NSLog(@"In ELSE::rows:: %d",[self.insertIndexPaths count]/2+1); 
    return [self.contactsArray count]/2 + 1; 
} 

}

+0

for循环有错误:: - for(presentrows; presentrows iOSAppDev 2012-04-04 07:47:19

+0

它现在没有崩溃,但它显示self.insertIndexPaths计数=> 0每次 – iOSAppDev 2012-04-04 08:16:23

+0

看到编辑答案 – 2012-04-04 08:35:40

0

真正的错误后终止应用程序上市,由于未捕获的异常
它说:部分0中的行数无效。更新(5)后现有部分中包含的行数必须等于更新前(0)中该部分中包含的行数加上或减去从该部分插入或删除的行数(10插入,0删除)

那么,什么问题?你有0行,然后插入10,现在说它有5个?没有任何意义。你应该检查你的-tableView:numberOfRowsInSection:实现。

我只是看到你想要在单行中显示两条记录。这没有意义,真的。它使该表视图动画变得不必要的复杂。
但是,如果你想坚持这一点,你可能不会告诉桌子你插入了10行而是5行。

+0

如何判断您插入的是10行还是5行?我没有得到这个。抱歉。 – iOSAppDev 2012-04-04 08:13:27

+0

'[contactsTableView insertRowsAtIndexPaths:self.insertIndexPaths withRowAnimation:UITableViewRowAnimationNone];'向表格中插入新单元格。由于该数组包含10个索引路径(在你的情况下),它将增加10个索引,即使你只需要5个。请每个元素使用一个单元格。这很容易。 – 2012-04-04 09:51:30

+0

如果我使用'for(noOfRowsCounter = noOfRowsCounter; noOfRowsCounter iOSAppDev 2012-04-04 10:02:52