';n内部一致性异常';在uitableview iphone中插入行时
在我的应用程序中,我使用';n内部一致性异常';在uitableview iphone中插入行时,iphone,uitableview,cell,rows,Iphone,Uitableview,Cell,Rows,在我的应用程序中,我使用NSNotificationCenter批量返回10个用户对象。当我得到10个对象时,我想在表视图中插入这些数据。为此,我使用以下代码 - (void)viewDidAppear:(BOOL)animated { recordCounter = 0; noOfRowsCounter = 0; // Some code } - (void) gotNotification: (NSNotification *) notification {
NSNotificationCenter
批量返回10个用户对象。当我得到10个对象时,我想在表视图中插入这些数据。为此,我使用以下代码
- (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;
}
}
我想在表视图中添加这些记录。我怎样才能做到这一点。谢谢
更新: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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
ContactCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
NSArray*cellArray=[[NSBundle mainBundle]loadNibNamed:@“ContactCell”所有者:自选项:nil];
//NSLog(@“CellRay count=>%d”,[CellRay count]);
单元格=[X射线对象索引:0];
}
//配置单元格。。。
//用于在单行中显示2条记录
int row=indexPath.row*2;
如果(第行)由于未捕获异常而终止应用程序后会列出实际错误。
它表示:节0中的行数无效。更新后现有节中包含的行数(5)必须等于更新前该节中包含的行数(0),加上或减去从该节中插入或删除的行数(插入10行,删除0行)
那么,问题出在哪里?您有0行,然后插入了10行,现在说它有5行?没有任何意义。您应该检查您的-tableView:numberofrowsin节:
实现
我只是看到您希望在一行中显示两条记录。这真的没有意义。这使得表视图动画变得不必要的复杂。
但是,如果您想坚持这样做,您可以不告诉表您插入了10行,而是5行。根据您的数据,您在节中返回了5行,但您的
self.insertIndexPaths有10个索引路径
[contactsTableView insertRowsAtIndexPaths:self.insertIndexPaths
withRowAnimation:UITableViewRowAnimationNone];
这些行指定插入10行,正如更新后得到的行数错误中明确提到的,应该等于第节中的行数返回值
更换管路
for (noOfRowsCounter = noOfRowsCounter; noOfRowsCounter < recordCounter; noOfRowsCounter++) {
[self.insertIndexPaths addObject:[NSIndexPath indexPathForRow:recordCounter inSection:0]];
}
}for循环有错误::-for(presentrows;presentrows0每次都会延迟回复。感谢上面的解决方案。你能告诉我一件事吗:-如果我快速滚动表视图,应用程序崩溃,因为我们没有那么多数据要显示。我如何克服这个问题?你能发布崩溃日志吗,以便我们可以清楚地检查错误。我在下面添加了崩溃日志,谁可以告诉表您插入了10行,但插入了5行?我不明白。抱歉。[contactsTableView insertRowsAtIndexPaths:self.insertIndexPaths with RowAnimation:UITableViewRowAnimationNone]
向表中插入新的单元格。由于数组包含10个索引路径(在您的情况下),即使您只需要5个索引路径,它也会添加10个索引路径。请每个元素使用一个单元格。这要容易得多。如果我对(noOfRowsCounter=noOfRowsCounter;noOfRowsCounter[self.insertIndexPaths addObject:[NSIndexPath indexPathForRow:recordCounter inSection:0]];}
在一行中显示2条记录是应用程序的要求,因此我无法更改它。我告诉过您问题所在。如果您坚持在每个单元格中显示两项,我无法帮助您。您有一个严重的设计问题。
for (noOfRowsCounter = noOfRowsCounter; noOfRowsCounter < recordCounter; noOfRowsCounter++) {
[self.insertIndexPaths addObject:[NSIndexPath indexPathForRow:recordCounter inSection:0]];
}
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]];
}
// 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;
}