0

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

 }

}
4

3 回答 3

0

当您删除行时,只需像@rohitUITableView那样再次将其排序为您想要的顺序,然后删除该行并根据顺序重新加载您的表。希望这可以帮助。

于 2013-06-12T11:59:10.217 回答
0

这是代码:-

Observation *observation = [self.observations objectAtIndex:[self.observations count]-1];
int lastValue = [observation.itemNo intValue];
lastValue++;

lastValue是您可以使用的最大值

希望对你有帮助

于 2013-01-04T12:25:20.600 回答
0

试试这个代码

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

 }

}

让我知道它是否不起作用

于 2013-06-12T11:52:26.340 回答