zoukankan      html  css  js  c++  java
  • 一个数据表对象(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];

            }


    }

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

  • 相关阅读:
    家谱树 x
    codevs 1231 最优布线问题 x(find函数要从娃娃抓起系列)
    洛谷 P1546 最短网络 Agri-Net x
    codevs 5969 [AK]刻录光盘x
    家谱(gen)x
    [POJ2594]Treasure Exploration(最小路径覆盖变种,floyd算法,匈牙利算法)
    [HDOJ5855]Less Time, More profit(最大权闭合子图,二分,最大流)
    [HDOJ1054]Strategic Game(最小点覆盖,最大二分匹配,HK算法)
    [HDOJ3829]Cat VS Dog(最大独立集)
    [HDOJ3488]Tour(二分图最小匹配,KM算法)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4750922.html
Copyright © 2011-2022 走看看