我有一个带有三个自定义(Xib)单元格的UITableview。我静态地将每个单元格加载到cellForRowAtIndexPath中。每个单元格都包含添加按钮以在下一行插入新行。当我插入新行时,它显示另一个Xib单元格而不是预期的单元格,并在所有部分中添加新行。如何在Objective C中解决它? 注意:
1)静态加载细胞。
2)但动态插入单元格。
3)在numberOfRowsInSection方法中,使用数组(NSMutableArray)计数给出行计数。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ if (indexPath.row==0)
{
CustomCellA *aCell = [tableView dequeueReusableCellWithIdentifier:@"ACell"];
return aCell;
} else if(indexPath.row==1)
{
CustomCellB *bCell = [tableView dequeueReusableCellWithIdentifier:@"BCell"];
return bCell;
}else
{
CustomCellC *cCell = [tableView dequeueReusableCellWithIdentifier:@"CCell"];
return cCell;
}
}
-(void)insertNewACell:(UIButton *)addButton atIndexPath:(NSIndexPath *)addBtnIndexPath
{
newArray = [[NSMutableArray alloc]init];
[newArray addObject:@"XXXX"];
[newArray addObject:@"YYYY"];
[newArray addObject:@"YYYY"];
[sectionRowItems addObject:newArray];
[sectionRowItems insertObject:newArray atIndex:addBtnIndexPath.row];
[self.numericTable reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return alphabetArray.count
}
所有你在其他部分设置CustomCellC
的拳头。所以,当你添加新单元格时,如果单元格indexpath.row
大于1则使用CustomCellC
可能是您为所有部分使用相同的数组。因此,当您在数组中插入新对象时,numberOfRowsInSection
将为每个部分返回相同的计数,因此在每个部分中添加新行。
您可以为每个部分设置不同的数组,并为特定部分设置行数,如下所示:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0){
return arr.count;
}else if (section == 1{
return arr1.count;
}
return arr2.count;
}
-(void)insertNewACell:(UIButton *)addButton atIndexPath:(NSIndexPath *)addBtnIndexPath
{
newArray = [[NSMutableArray alloc]init];
[newArray addObject:@"XXXX"];
[newArray addObject:@"YYYY"];
[newArray addObject:@"YYYY"];
if(addBtnIndexPath.section)
[arr addObject:newArray]
}else if(addBtnIndexPath.section){
[arr1 addObject:newArray]
}else{
[arr addObject:newArray]
}