Objective c 删除行后如何对表视图编号字符串进行数字排序

Objective c 删除行后如何对表视图编号字符串进行数字排序,objective-c,ios,cocoa-touch,uitableview,Objective C,Ios,Cocoa Touch,Uitableview,我有一个表视图,可以在按下按钮时添加行。每行从字符串itmeNo按升序编号。我的问题是,当用户删除一行时,数字不再按顺序排列 即1,2,3,4,5 如果用户删除第3行和第4行,则会对它们进行编号 1,2,5 我对如何进行数字排序有点困惑?其中我有[self.observations count]+1]我可以更改此选项吗?itemNo总是按数字升序排序 总之,我总是希望我的表视图行数字符串总是按顺序编号 有点触手可及,但到目前为止,我尝试了[self.tableView reloadData],并

我有一个表视图,可以在按下按钮时添加行。每行从字符串
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];

 }

}
以下是代码:-

Observation *observation = [self.observations objectAtIndex:[self.observations count]-1];
int lastValue = [observation.itemNo intValue];
lastValue++;
lastValue
是您可以使用的最大值

希望它能帮助您

尝试此代码

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

//need to add these lines
 NSSortDescriptor *sortByUploaded =[NSSortDescriptor sortDescriptorWithKey:@"itmeNo"ascending:YES];

  NSArray *sortDescriptorArr = [NSArray arrayWithObjects:sortByUploaded,nil];

 self.observations = [NSMutableArray arrayWithArray:[self.observations sortedArrayUsingDescriptors:sortDescriptorArr]];


    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

 }
}


当您从
UITableView
中删除行时,如果它不起作用,请告诉我,只需按照@strong>rohit所做的顺序重新排序,然后删除该行并根据顺序重新加载表即可。希望这能有所帮助。

您应该检索id的上一个数组objectAtIndex值,然后向其中添加+1,然后存储新值。.感谢您的快速回复,您能否对此进行一些扩展,因为我不太清楚我粘贴了下面的代码1,2,5是否仍然正常,它只是不连续,并且它与记录的顺序不匹配。再次感谢您,不幸的是,这没有对我的表视图编号字符串重新排序。当然,除非我没有正确地实现它,否则它是一样的。我尝试了我的delete方法和add方法我将它添加到了
//delete observations
方法中,但不起作用,所以我将它添加到了,
-(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatindexpath:(nsindepath*)indepath
在addObservationButtonPressed函数中添加此代码并替换此行观察。itemNo=[NSString stringWithFormat:@“%d”,[self.observations count]+1];with observation.itemNo=[NSString stringWithFormat:@“%d”,lastValue];这会使应用程序崩溃,感谢您的帮助。另外,它会在应用程序崩溃的哪一行重复*observation?