Objective c 即使在使用insertRowsAtIndexPaths方法之前更新数据源,也无法将单元格插入UITableView
我正在尝试在事件表视图中点击的单元格下方添加一个新单元格:didSelectRowAtIndexPathObjective c 即使在使用insertRowsAtIndexPaths方法之前更新数据源,也无法将单元格插入UITableView,objective-c,cocoa-touch,uitableview,Objective C,Cocoa Touch,Uitableview,我正在尝试在事件表视图中点击的单元格下方添加一个新单元格:didSelectRowAtIndexPath - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @"SWTableCell"; UITableViewCell * cell = [tableView dequ
- (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;
}
这些是我的NumberOfSectionsTableView和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]时,Methodos 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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“SWTableCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
UILabel*lblTemp;//用于为单元格创建标签
CGRect重新调整,rectTemp;
rectemp=CGRectMake(0,0,self.frame.size.width,15);//单元格的大小
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
cell.frame=rectemp;
int x=10;//每个标签的初始x位置
for(int i=0;i
还有什么会导致这个问题呢?我想
- (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];
}
你能给我们看一下
表格视图:cellforrowatinexpath:
?你是对的,但是,我有-1,因为我不想使用数据表的第一行。在我的CellForRowatinex方法中,我将+1添加到indexPath.row以获得正确的行信息。我将更新问题。如果您的意思是,您不想使用第一个单元格,那么它必须是+1I使用-1,因为第一行不计算在内。在cellForRowAtIndexPath方法中,我使用indexRow.row+1。我的cellForRowAtIndexPath有点乱,不过我会把它添加到问题中。row是什么意思?如果它是tableView的第一个单元格,那么它必须是+1,如果不想使用数组中的第一项,那么它应该是-1。或者你应该删除它。我不想使用数组中的第一项,所以它是-1。对于未来的对话,行=数组,单元格=表格视图。