點擊表格視圖頂部的「+」按鈕後,我插入一個新行。它加入罰款。但是當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];
}
正在發生的事情,並在滾動會發生什麼?他們如何重新排序? – Rikh
(考慮我加了8行)單元格的順序應該總是像「row1,row2,row3,row4,row5,row6,row7,row8」,但它隨機變化,像「row1,row2,row3,row4,row8,滾動後的「row7,row6,row5」。 –
嘗試答覆Nirav D發佈,這可能是因爲條件'if(cell == nil)'條件未被滿足,如果單元格被重新使用,因此你的textLabel沒有得到更新。但即使如此,你應該看到重複的文本,而不是單元格出現重新排序。儘管如此。 – Rikh