如何处理具有相同标识符的动态插入的UITableView单元格?

时间:2017-04-15 05:10:56

标签: ios objective-c uitableview

我在" +"时插入新行单击表视图顶部的按钮。它正在增加罚款。但是当tableview上下滚动时,单元格会重新排序。有办法处理这个吗?这是完整的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"TABLE";
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    array1 = [[NSMutableArray alloc]initWithObjects:@"Row1",@"Row2",@"Row3",@"Row4", nil];

    table1 = [[UITableView alloc]initWithFrame:CGRectMake(20, 20, 360, 360)style:UITableViewStyleGrouped];
    table1.delegate = self;
    table1.dataSource = self;
    table1.backgroundColor = [UIColor brownColor];
    [self.view addSubview:table1];

    UIBarButtonItem *plusButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(plusButtonHit)];
    self.navigationItem.rightBarButtonItem = plusButton;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array1 count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SecondViewController *secondViewController = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondViewController animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSUInteger row = [indexPath row];
    NSUInteger count = [array1 count];

    if (row < count)
    {
        return UITableViewCellEditingStyleDelete;

    } else
    {
        return UITableViewCellEditingStyleNone;
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
    [array1 removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } 
}

- (void)plusButtonHit
{

    NSIndexPath *indexPath = [[NSIndexPath alloc]init];
    indexPath = [NSIndexPath indexPathForRow:array1.count inSection:0];
    NSString *newString = [NSString stringWithFormat:@"Row%ld", (long)indexPath.row+1];
    [array1 addObject:newString];
   [table1 insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

}

2 个答案:

答案 0 :(得分:1)

在if条件块之后的cellForRowAt方法设置单元格标签text

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
//Set cell's label text here
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
return cell;

答案 1 :(得分:0)

更改代码如下:设置 reuseIdentifier:nil

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];