2017-04-15 28 views
1

点击表格视图顶部的“+”按钮后,我插入一个新行。它加入罚款。但是当tableview向上和向下滚动时,单元格会重新排序。有什么办法可以解决这个问题吗?这是完整的代码。如何处理具有相同标识符的动态插入的UITableView单元?

- (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]; 

} 
+1

正在发生的事情,并在滚动会发生什么?他们如何重新排序? – Rikh

+0

(考虑我加了8行)单元格的顺序应该总是像“row1,row2,row3,row4,row5,row6,row7,row8”,但它随机变化,像“row1,row2,row3,row4,row8,滚动后的“row7,row6,row5”。 –

+1

尝试答复Nirav D发布,这可能是因为条件'if(cell == nil)'条件未被满足,如果单元格被重新使用,因此你的textLabel没有得到更新。但即使如此,你应该看到重复的文本,而不是单元格出现重新排序。尽管如此。 – Rikh

回答

1

在如果条件块之后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; 
+0

是的...对不起,我以为你做了,我不知道人们有什么问题......他们会点击downvotes。我非常喜欢这个。 @NiravD –

+0

@NAVEENKUMAR可能是你设置'reuseIdentifier'为零的原因,这将增加大量的内存使用,因为它每次创建新的单元格 –

+0

是的,我知道但是..对于有限的单元格,它不会影响内存由于单元的大小,可重用的标识符会创建新的单元格。对于她来说,它不会影响内存大小。 –

0

改变这样的代码:设置reuseIdentifier:无

-(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]; 
+0

你的解决方案工作正常。谢谢 。 –

+0

谢谢你... @HemaVathi –

相关问题