我有一个表格视图,可以在按下按钮时添加行。对于每一行,行按字符串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];
}
}
答案 0 :(得分:0)
这是代码: -
Observation *observation = [self.observations objectAtIndex:[self.observations count]-1];
int lastValue = [observation.itemNo intValue];
lastValue++;
lastValue
是您可以使用的最大值
希望它可以帮到你
答案 1 :(得分: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];
}
}
如果它不起作用,请告诉我
答案 2 :(得分:0)
当您从UITableView
删除行时,只需将其重新排序为@ rohit 所需的订单,然后删除该行并根据订单重新加载您的表。希望这会有所帮助。