[英]how to change the index of the CoreData when reordering the TableView?
Please go through the code and tell me what i am doing wrong here. 请检查代码,并告诉我我在这里做错了什么。
TableViewController.m TableViewController.m
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Device" inManagedObjectContext:context];
NSFetchRequest *fetchrequest = [[NSFetchRequest alloc]initWithEntityName:entity];
_devices = [[context executeFetchRequest:fetchrequest error:nil]mutableCopy];
[self.tableView reloadData]; // When TableView is reloading the data in tableview updating according to the data stored via CoreData.
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
if (fromIndexPath != toIndexPath ) {
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *device = [self.devices objectAtIndex:fromIndexPath.row];
NSString *str1 = [device valueForKey:@"text1"];
NSLog(@"%@",str1);
[self.devices removeObject:device];
[self.devices insertObject:device atIndex:[toIndexPath row]];
// int i = 0;
// for (NSManagedObject *mo in self.devices)
// {
// NSManagedObject *new = [self.devices objectAtIndex:i];
// NSString *check = [new valueForKey:@"text1"];
// NSLog(@"%@",check);
// NSString *check1 = [new valueForKey:@"text2"];
// NSLog(@"%@",check1);
// NSString *check2 = [new valueForKey:@"text3"];
// NSLog(@"%@",check2);
// [mo setValue:[NSString stringWithFormat:@"%@",[new valueForKey:@"text1"]] forKey:@"text1"];
// [mo setValue:[NSString stringWithFormat:@"%@",[new valueForKey:@"text2"]] forKey:@"text2"];
// [mo setValue:[NSString stringWithFormat:@"%@",[new valueForKey:@"text3"]] forKey:@"text3"];
// i++;
// }
NSError *error;
if(![context save:&error]){
NSLog(@"%@ %@",error, [error localizedDescription]);
}
}
}
I have entity Name: Device and with column names: text1,text2,text3 我有实体名称:Device,列名称:text1,text2,text3
I don't want to sort the data in Core Data, actually i want to move the rows in TableView and i want to update the index of the data saved in columns text1,text2,text3. 我不想对核心数据中的数据进行排序,实际上我想移动TableView中的行,并且我想更新保存在text1,text2,text3列中的数据的索引。 In total, there are 5 rows in tableview and i just want to update the index of the values with respect to the tableview index which is updated.
总的来说,tableview中有5行,我只想相对于已更新的tableview索引更新值的索引。
Check below tutorial 检查下面的教程
http://www.cimgf.com/2010/06/05/re-ordering-nsfetchedresultscontroller/ http://www.cimgf.com/2010/06/05/re-ordering-nsfetchedresultscontroller/
for short you need to add "displayIndex" property to the objects you are trying to reoder 简而言之,您需要将“ displayIndex”属性添加到您要整理的对象中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.