冲突

2017-12-18 426 views
-1

我有一个带有三个自定义(Xib)单元的UITableview。我静态加载了cellForRowAtIndexPath中的每个单元格。每个单元格包含添加按钮以在下一行插入新行。当我插入新行时,它会显示另一个Xib单元而不是预期的单元格,并且还会在所有节中添加新行。如何在Objective C中解决它?
注:冲突

1)单元格静态加载。

2)但动态插入单元格。

3)在numberOfRowsInSection方法中,使用数组(NSMutableArray)count给出的count个计数。

-(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 
} 

回答

0

所有你正在设置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] 
    } 
+0

谢谢您的建议来管理部分。好友我如何管理cellForRowAtIndexPath当我插入新行,如上所述。 –

+0

你可以应用像numberOfRowsInSection这样的条件,就像我在我的回答中一样。 –

+0

对不起,如果你想为每个部分使用同一个单元格,你不需要在cellForRowAtIndexPath中做任何修改。 –