我有一个表格视图,可以在按下按钮时添加行。行从字符串itmeNo
中按每行的升序编号。我的问题是当用户删除一行时,数字不再按顺序排列。
即 1,2,3,4,5,
如果用户删除第 3 行和第 4 行,它们将被编号
1,2,5
我有点坚持如何进行数字排序?我在哪里[self.observations count] + 1];
可以改变这个也许itemNo
总是按数字升序排序
总之,我总是希望我的表格视图行编号字符串始终按顺序编号
有点影响,但到目前为止,我尝试[self.tableView reloadData];
了删除方法并使用了允许重新排序表视图的方法。
- (void)addObservationButtonPressed:(id)sender
{
LogCmd();
Observation *observation = [[ICObservationManager manager] newObservation];
observation.createdAt = [NSDate date];
observation.modifiedAt = [NSDate date];
observation.certificate = self.certificate;
observation.itemNo = [NSString stringWithFormat:@"%d", [self.observations count] + 1];
[self.certificate addObservationsObject:observation];
[self loadData];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.observations.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.certificate.observations count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"ObservationsCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Observation *observation = [self.observations objectAtIndex:indexPath.row];
NSString *itemLabel = [NSString stringWithFormat:@"%@. %@",
observation.itemNo,
(observation.item.length ? observation.item : @"No description")];
cell.textLabel.text = itemLabel;
return cell;
}
//Delete observations
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.observations objectAtIndex:indexPath.row]];
[self.appDelegate saveContext];
[self.observations removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}