[英]How to sort table view number string numerically after row deletion
I have a table view that adds rows on a button press. 我有一个表格视图,可以在按下按钮时添加行。 The rows are numbered from a string
itmeNo
in ascending order for each row. 每行从字符串
itmeNo
升序编号。 My problem is when user deletes a row, the numbers are no longer in order. 我的问题是,当用户删除一行时,数字不再按顺序排列。
ie 1,2,3,4,5, 即1,2,3,4,5,
if user deletes row 3and 4 they are numbered 如果用户删除第3行和第4行,则会对其进行编号
1,2,5 1,2,5
Im a bit stuck on how to sort numerically? 我对如何进行数字排序有些困惑? where I have
[self.observations count] + 1];
我的
[self.observations count] + 1];
can I change this perhaps so itemNo
always sorts ascending numerically 我可以更改一下
itemNo
所以itemNo
总是按数字升序itemNo
In summary I always want my table view rows number string to always be sequentially numbered 总之,我总是希望我的表格视图行号字符串始终按顺序编号
A bit of reach but so far ive tried [self.tableView reloadData];
有点影响,但到目前为止,我尝试过
[self.tableView reloadData];
in the delete method and playing around with method allowing re ordering of table view. 在delete方法中进行操作,并使用允许重新排序表视图的方法。
- (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];
}
}
Here is code :- 这是代码:-
Observation *observation = [self.observations objectAtIndex:[self.observations count]-1];
int lastValue = [observation.itemNo intValue];
lastValue++;
lastValue
is max value you can use lastValue
是您可以使用的最大值
Hope it helps you 希望对您有帮助
Try this code 试试这个代码
- (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];
}
} }
Let me know if its not working 让我知道它是否不起作用
When you are deleting rows from UITableView
just sort it to the order again you want as @ rohit does and then delete the row and reload your table according to order. 当您从
UITableView
删除行时,只需像@ rohit一样将其再次排序为所需的顺序,然后删除该行并根据顺序重新加载表格。 Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.