zoukankan      html  css  js  c++  java
  • TableViewController的添加,删除,移动

    #import "RootTableViewController.h"

     

    @interface RootTableViewController ()

    {

        UITableViewCellEditingStyle _style;

    }

    @property(nonatomic,strong)NSMutableArray *array;

    @end

     

    @implementation RootTableViewController

     

    - (id)initWithStyle:(UITableViewStyle)style

    {

        self = [super initWithStyle:style];

        if (self) {

            // Custom initialization

            self.array=[NSMutableArray array];

            

               }

        return self;

    }

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        

        //在数组里存一些数据

        for (int i=0; i<3; i++) {

            NSMutableArray *tempArray=[NSMutableArray array];

            for (int j=0; j<5; j++) {

                [tempArray addObject:[NSString stringWithFormat:@"%d,%d个人",i,j]];

            }

            [self.array addObject:tempArray];

        }

       // 设置代理

        self.tableView.dataSource=self;

        self.tableView.delegate=self;

     

        //在导航栏两侧添加两个barbutton

        UIBarButtonItem *bar=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStyleDone) target:self action:@selector(barAction:)];

        self.navigationItem.leftBarButtonItem=bar;

        

        UIBarButtonItem *bar1=[[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(barAction1:)];

        self.navigationItem.rightBarButtonItem=bar1;

     

        

        

    }

    //barbutton编辑事件

    -(void)barAction:(UIBarButtonItem *)sender

    {

        _style=UITableViewCellEditingStyleDelete;

        BOOL flag=self.tableView.editing;

        [self.tableView setEditing:!flag animated:YES];

    }

    //barbutton添加事件

    -(void)barAction1:(UIBarButtonItem *)sender

    {

        _style=UITableViewCellEditingStyleInsert;

        BOOL flag=self.tableView.editing;

        [self.tableView setEditing:!flag animated:YES];

    }

    //设置哪些行能编辑(默认全部都能)

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return  YES;

    }

    //确定编辑状态(删除|添加)

    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return _style;

    }

    //提交编辑

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if(editingStyle==UITableViewCellEditingStyleDelete)

        {

            //删除

            [self.array[indexPath.section]removeObjectAtIndex:indexPath.row];

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

        }

        else

        {

            //添加

            NSString *s=@"测试数据";

            [self.array[indexPath.section]insertObject:s atIndex:indexPath.row+1];

            NSIndexPath *index=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

            [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationRight)];

            

        }

        

    }

    //哪些行能移动

    -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return YES;

    }

     

    //完成移动

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

    {

        //先赋值

        id obj=self.array[sourceIndexPath.section][sourceIndexPath.row];

        //再删除

        [self.array[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];

        //最后添加

        [self.array[destinationIndexPath.section]insertObject:obj atIndex:destinationIndexPath.section];

    }

    //检查越界

    -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

    {

        

        if(sourceIndexPath.section!=proposedDestinationIndexPath.section)

        {

            return sourceIndexPath;

        }

        else

        {

            return proposedDestinationIndexPath;

        }

    }

     

  • 相关阅读:
    tyvj 1031 热浪 最短路
    【bzoj2005】 [Noi2010]能量采集 数学结论(gcd)
    hdu 1394 Minimum Inversion Number 逆序数/树状数组
    HDU 1698 just a hook 线段树,区间定值,求和
    ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
    ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
    ZeptoLab Code Rush 2015 A. King of Thieves 暴力
    hdoj 5199 Gunner map
    hdoj 5198 Strange Class 水题
    vijos 1659 河蟹王国 线段树区间加、区间查询最大值
  • 原文地址:https://www.cnblogs.com/haiying/p/4083393.html
Copyright © 2011-2022 走看看