我正在尝试在事件表上的tapped单元格下面添加一个新单元格:didSelectRowAtIndexPath
这些是我的numberOfSectionsInTableView和numberOfRowsInSection方法。 self.dataTable属性是我的TableView的数据源。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//i have that - 1 there because i don't want to use the first row of my dataTable.
//On my cellForRowAtIndex method I'm adding a +1 to the indexPath.row to get the right row information
return [self.dataTable count] - 1;
}
我的添加方法是:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray * newRow = [NSMutableArray arrayWithObjects:@"Columna 1", @"Columna 2", @"Columna 3", @"Columna 4", nil];
//here im adding the new item to the datasource.
[self.dataTable insertObject:newRow atIndex:indexPath.row + 1];
NSArray *newData = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:1], nil];
//self because this clas inherits from UITableView and implements its own delegate and datasource.
//The error is also produced using tableView instead of self.
[self beginUpdates];
[self insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];
[self endUpdates];
}
当debbugger到达[self endUpdates]时,启动了methos numberOfRowsInSection,我可以看到返回更新为+1,但是我得到了这个错误:
异常名称:NSInternalInconsistencyException原因:无效的表视图更新。应用程序已请求更新表视图,该更新与数据源提供的状态不一致。
编辑:添加cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"SWTableCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
UILabel *lblTemp; //Used for creates labels for cell
CGRect readjust, rectTemp;
rectTemp = CGRectMake(0, 0, self.frame.size.width, 15); //Size of the cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.frame = rectTemp;
int x = 10; //initial x position for each label
for(int i = 0; i < self.columnCount; i++)
{
rectTemp = CGRectMake(x, 10, COLUMN_WIDTH, 20); //Size of each column
lblTemp = [[UILabel alloc] initWithFrame:rectTemp]; //inicialize label
lblTemp.tag = i;
[lblTemp setUserInteractionEnabled:NO];
lblTemp.text = (NSString *)[[dataTable objectAtIndex:indexPath.row + 1] objectAtIndex:i];
lblTemp.autoresizingMask= UIViewAutoresizingFlexibleRightMargin;
[lblTemp setHighlightedTextColor:[UIColor whiteColor]];
if(i!=0) lblTemp.textAlignment = UITextAlignmentRight;
else lblTemp.textAlignment = UITextAlignmentLeft;
lblTemp.font = [UIFont systemFontOfSize:10];
[cell setUserInteractionEnabled:YES];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[lblTemp setNumberOfLines:1];
[lblTemp sizeToFit];
readjust = lblTemp.frame;
readjust = CGRectMake(readjust.origin.x, readjust.origin.y, COLUMN_WIDTH, readjust.size.height);
lblTemp.frame = readjust;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
x = x + COLUMN_WIDTH;
}
}
return cell;
}
还有什么可能导致此问题?
答案 0 :(得分:0)
我认为
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataTable count] - 1;
}
应该是
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataTable count];
}
集合的找到或长度是它包含的项目数。只有索引从0开始。所以如果你在一个数组中放了5个项目,它就有5个,而最后一个项目的索引是4个。
现在我也失明了:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//…
[self beginUpdates];
[self insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];
[self endUpdates];
}
必须是
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//…
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];
[tableView endUpdates];
}