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];

}

}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. web开发学习之Http协议
  2. MySQL创建数据库和表的Demo
  3. 产品列表页分类筛选、排序的算法实现(PHP)
  4. js初学—js全自定义单选框
  5. JQuery一些基础笔记
  6. PHP preg_match正则表达
  7. 2014.8.3情人节欢乐赛【Benny的农场】
  8. hadoop 提高hdfs删文件效率----hadoop删除文件流程解析
  9. 使用Retrofit和RxJava
  10. Linux下修改Swap分区大小
  11. [LeetCode] Decode Ways II 解码方法之二
  12. C语言 &gt; 构造素数表
  13. 多个yml文件的读取方式
  14. CSS3 transform-style 属性
  15. Vue(十七)模块化开发
  16. Java的OOP三大特征之一——多态
  17. C语言 &#183; 递归求二项式系数值
  18. C#实现支持单点登录的一个存储用户信息的类
  19. 最小化安装CentOS7,没有ifconfig命令---yum search command_name搜索未知包名
  20. 在Spark程序中使用压缩

热门文章

  1. 【Android 应用程序开发】 Fragment 详细说明
  2. go语言实现遍历目录,及查找特定的文件类型
  3. oracle表空间查询维护命令大全之三(暂时表空间)史上最全
  4. MySQL 最经常使用的一千行
  5. 重写onBackPressed方法
  6. 使用clojure訪问SQL Server数据库
  7. Duanxx的STM32学习: 启动模式,BOOT0和BOOT1具体解释
  8. Directx11学习笔记【十六】 光照模型的实现
  9. 利用Sails.js+MongoDB开发博客系统
  10. String和StringBuffer 常用方法总结