衝突

2017-12-18 425 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中做任何修改。 –