一个数据表对象(NSManagedObject)加入排序
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];
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
最新文章
- web开发学习之Http协议
- MySQL创建数据库和表的Demo
- 产品列表页分类筛选、排序的算法实现(PHP)
- js初学—js全自定义单选框
- JQuery一些基础笔记
- PHP preg_match正则表达
- 2014.8.3情人节欢乐赛【Benny的农场】
- hadoop 提高hdfs删文件效率----hadoop删除文件流程解析
- 使用Retrofit和RxJava
- Linux下修改Swap分区大小
- [LeetCode] Decode Ways II 解码方法之二
- C语言 >; 构造素数表
- 多个yml文件的读取方式
- CSS3 transform-style 属性
- Vue(十七)模块化开发
- Java的OOP三大特征之一——多态
- C语言 &#183; 递归求二项式系数值
- C#实现支持单点登录的一个存储用户信息的类
- 最小化安装CentOS7,没有ifconfig命令---yum search command_name搜索未知包名
- 在Spark程序中使用压缩