一个数据表对象(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];
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
相关文章
- 万万没想到,go也能使用对象来操作数据库了,网友直呼:健壮性有保证了
- Javaweb | ServletContext对象
- 在 jQuery 中如何判断对象是否存在
- MongoDB用Mongoose得到的对象不能增加属性完美解决方法(两种)
- java8 按对象属性值排序详解编程语言
- Java8从对象列表中取出某个属性的列表详解编程语言
- Spring saveOrUpdate方法:保存或更新实体对象
- Oracle对象解开未知的神秘面纱(.oracle对象是什么)
- Oracle中的寄存对象灵活的闭环实现(oracle中的寄存对象)
- 利用Redis优化对象排序的性能(redis缓存的对象排序)
- [ASP.NETAJAX]Function对象及Type类的方法介绍
- ORACLE常用的SQL语法和数据对象
- JS对象转换为Jquery对象示例
- jquery获取当前点击对象的value方法
- 一个JavaScript函数把URL参数解析成Json对象