IOS:对表视图进行编辑

    1、可以对表视图的每行进行编辑,如删除一行,增加一行,进行移动等。

     首先创建一个单视图工程,在此基础上给视图添加一个导航栏。

     然后,在ViewController.h文件里的代码如下:

      

#import <UIKit/UIKit.h>


@interface ViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>


@property(retain,nonatomic)UITableView *mTableView;


@property(retain,nonatomic)NSMutableArray *marr;


@end

     

     

     在ViewController.m文件里的代码如下:


@implementation ViewController


//重写父类方法

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   if([superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil])

    {

        //设置导航栏标题

        self.navigationItem.title =@"首页";

        //创建一个导航栏上的按钮

        UIBarButtonItem *pBtn = [[UIBarButtonItemalloc]initWithTitle:@"BegEdit"style:UIBarButtonItemStyleDonetarget:self

   action:@selector(tableViewEdit:)];

        //将按钮添加到导航栏右边

        self.navigationItem.rightBarButtonItem = pBtn;

    }

    

    return self;

}


//导航栏按钮关联的方法

- (void)tableViewEdit:(id)sender

{

    //设置导航栏的按钮

    [sendersetTitle:[self.mTableViewisEditing]?@"EndEdit":@"BegEdit"];

    //改变表视图的编辑状态

    [self.mTableViewsetEditing:![self.mTableViewisEditing]];

}

- (void)viewDidLoad

{

    [superviewDidLoad];

    //初始化数组

    self.marr = [NSMutableArrayarrayWithCapacity:5];

    //给数组赋值

   for(int i=0;i<5;i++)

    {

       NSString *pstr = [NSStringstringWithFormat:@"%d",i];

        

        [self.marraddObject:pstr];

    }

    //创建初始化表视图

    self.mTableView = [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];

    //设置代理

   self.mTableView.dataSource =self;

    

   self.mTableView.delegate =self;

    

    //将表视图添加到视图中

    [self.viewaddSubview:self.mTableView];

    

}


//设置当前表格是否可以被编辑

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

{

    //第一行不能编辑

   if([indexPathrow] ==0)

    {

       return NO;

    }

    //其他行可以编辑

    return YES;

}


//每行的编辑方式

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

{

    //偶数行编辑方式为删除

   if([indexPathrow] %2)

    {

        returnUITableViewCellEditingStyleDelete;

    }

    //奇数行编辑行为是添加

    returnUITableViewCellEditingStyleInsert;

}


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

{

    if(editingStyle ==UITableViewCellEditingStyleDelete)

    {

        //从数组中移除该对象

        [self.marrremoveObjectAtIndex:[indexPathrow]];

        //表视图开始更新

        [self.mTableViewbeginUpdates];

        //在表中移除该对象

        [self.mTableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationRight];

        //表视图结束更新

        [self.mTableViewendUpdates];

    }

    //如果编辑方式为添加

    elseif(editingStyle ==UITableViewCellEditingStyleInsert)

    {

        //在数组中添加新元素

        [self.marrinsertObject:@"newCell"atIndex:[indexPathrow]];

        //表视图开始更新

        [self.mTableViewbeginUpdates];

        //在表中加入新的一行

        [self.mTableViewinsertRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationBottom];

        //表视图结束更新

        [self.mTableViewendUpdates];

    }

}

//设置每行是否可以移动

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

{

   if([indexPathrow] ==2)

    {//第三行不能移动

       returnNO;

    }

    

    return YES;

}

//具体移动的实现

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

{

    //获取移动前的行

   NSInteger fromRow = [sourceIndexPathrow];

    //将要移动到的行

   NSInteger toRow = [destinationIndexPathrow];

    //获取数组中移动前下标对应的对象

   id obj = [self.marrobjectAtIndex:fromRow];

    //从数组中删除

    [self.marrremoveObjectAtIndex:fromRow];

    //在新的位置插入

    [self.marrinsertObject:objatIndex:toRow];

}


#pragma ------------------DataSource Delegate----------------------------------

//每个分组的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

   return [self.marrcount];

}

//渲染每行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   staticNSString *pstr =@"hello";

    //声明一个cell对象

    UITableViewCell *pCell = [tableViewdequeueReusableCellWithIdentifier:pstr];

    //cell为空,就创建它

   if(nil == pCell)

    {

        pCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:pstr];

    }

    //用来纪录当前行数

   NSInteger rowCell = [indexPathrow];

    

    pCell.textLabel.text = [self.marrobjectAtIndex:rowCell];

    

    pCell.textLabel.textColor = [UIColorgreenColor];

    

    pCell.detailTextLabel.text =@"detailTextField";

    

    pCell.detailTextLabel.textColor = [UIColorblueColor];

    

   return pCell;

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


   运行结果:
  
    点击按钮后,可以看到按钮标题变了。并且出了第一行之外,每一行都可以编辑(第3行不能进行移动)。

   

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值