一个数据表对象(NSManagedObject)加入排序
2015-08-22 19:36
513 查看
eg:数据库表对象
@interface Meditation :
NSManagedObject
@property (nonatomic,
retain) NSString * order;//用来排序的属性值。用0、1、2、3...排序
@end
//在这种方法里操作
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
Meditation *sourceMeditation=[self.dataSource
objectAtIndex:sourceIndexPath.row];
sourceMeditation.order =[NSString
stringWithFormat:@"%i",destinationIndexPath.row];
[sourceMeditation.managedObjectContext
save:nil];
//两种情况一种是上移,第二种是下移
1.sourceIndexPath.row>destinationIndexPath.row
2.sourceIndexPath.row>destinationIndexPath.row
if (sourceIndexPath.row < destinationIndexPath.row)
{
for (NSInteger i = sourceIndexPath.row+1; i <= destinationIndexPath.row; i ++)
{
Meditation *m = [self.dataSource
objectAtIndex:i];
m.order =[NSString
stringWithFormat:@"%i",[m.attribute6
integerValue]-1];
[sourceMeditation.managedObjectContext
save:nil];
}
}else
{
for (NSInteger i = sourceIndexPath.row-1; i >= destinationIndexPath.row; i--)
{
Meditation *m = [self.dataSource
objectAtIndex:i];
m.order =[NSString
stringWithFormat:@"%i",[m.attribute6
integerValue]+1];
[sourceMeditation.managedObjectContext
save:nil];
}
}
}
//当对象被删除的时候(依照order的升序排列)。把删除数据以下的数据对象的order属性值-1
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle ==
UITableViewCellEditingStyleDelete) {
for (int i=indexPath.row; i<self.dataSource.count; i++) {
Meditation *m = [self.dataSource
objectAtIndex:i];
m.order =[NSString
stringWithFormat:@"%i",[m.attribute6
integerValue]-1];
[appDelegate saveContext];
}
}
@interface Meditation :
NSManagedObject
@property (nonatomic,
retain) NSString * order;//用来排序的属性值。用0、1、2、3...排序
@end
//在这种方法里操作
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
Meditation *sourceMeditation=[self.dataSource
objectAtIndex:sourceIndexPath.row];
sourceMeditation.order =[NSString
stringWithFormat:@"%i",destinationIndexPath.row];
[sourceMeditation.managedObjectContext
save:nil];
//两种情况一种是上移,第二种是下移
1.sourceIndexPath.row>destinationIndexPath.row
2.sourceIndexPath.row>destinationIndexPath.row
if (sourceIndexPath.row < destinationIndexPath.row)
{
for (NSInteger i = sourceIndexPath.row+1; i <= destinationIndexPath.row; i ++)
{
Meditation *m = [self.dataSource
objectAtIndex:i];
m.order =[NSString
stringWithFormat:@"%i",[m.attribute6
integerValue]-1];
[sourceMeditation.managedObjectContext
save:nil];
}
}else
{
for (NSInteger i = sourceIndexPath.row-1; i >= destinationIndexPath.row; i--)
{
Meditation *m = [self.dataSource
objectAtIndex:i];
m.order =[NSString
stringWithFormat:@"%i",[m.attribute6
integerValue]+1];
[sourceMeditation.managedObjectContext
save:nil];
}
}
}
//当对象被删除的时候(依照order的升序排列)。把删除数据以下的数据对象的order属性值-1
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle ==
UITableViewCellEditingStyleDelete) {
for (int i=indexPath.row; i<self.dataSource.count; i++) {
Meditation *m = [self.dataSource
objectAtIndex:i];
m.order =[NSString
stringWithFormat:@"%i",[m.attribute6
integerValue]-1];
[appDelegate saveContext];
}
}
相关文章推荐
- Object-C 函数定义 -- 笔记
- iOS学习笔记-Objective-C-类和对象
- Object-C变量作用域 -- 笔记
- Object-C 对象 (创建/销毁 对象)-- 笔记
- Object-C 类定义 -- 笔记
- ViewModelBase && ObservableObject
- Objective-C 里的 Alloc 和 AllocWithZone的区别
- JS通过Object创建简单对象
- Objective-C基础知识点
- Objective-C基础知识点
- Objective-C基础知识点
- Objective-C基础知识点
- Objective-C基础知识点
- 通讯录
- 操作队列
- 变量对象(Variable Object)
- 【Objective-C】Copy语法
- 如何制作 Objective-C 的UML图 [2]
- Objective-C( 语法一)
- 读书笔记:Objective-C高级编程 iOS与OS X多线程和内存管理 ——(持续)