IOS5基础十六-----可移动的行和可删除的行

插入、移动 和删除行 这些都可以通过表视图中setEditing:animated实现。

里面做了一些简单的注释,基本可以理解。

#import "BIDSecondLevelViewController.h"

@interface BIDMoveMeController :BIDSecondLevelViewController

@property (strong,nonatomic)NSMutableArray *list;

-(IBAction)toggleMove;

@end


#import "BIDMoveMeController.h"

@implementation BIDMoveMeController

@synthesize list;

-(IBAction)toggleMove

{

    //切换到编辑模式

    [self.tableViewsetEditing:!self.tableView.editinganimated:YES];

    if(self.tableView.editing)

        [self.navigationItem.rightBarButtonItemsetTitle:@"Done"];

   else

        [self.navigationItem.rightBarButtonItemsetTitle:@"Move"];

}


-(void) viewDidLoad

    [superviewDidLoad];

     //加载数据

   if (list==nil) {

        NSMutableArray *array= [[NSMutableArrayalloc]initWithObjects:@"Eeny",@"Meeny",@"Miney",@"Mac",@"Catch",@"A",@"Tiger",@"By",@"The",@"Toe",nil];

       self.list=array;

    }

  //创建一个按钮栏项目设置标题为Move  style需要一个标准的右边栏  targetaction告知单击按钮做什么

    UIBarButtonItem *moveButton =[[UIBarButtonItemalloc]initWithTitle:@"Move"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(toggleMove)];

    self.navigationItem.rightBarButtonItem = moveButton;       

}


#pragma mark -

#pragma mark Table Data Source Methods

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

{

   return [listcount];

}


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

{

   staticNSString *MoveMeCellIdentifier =@"MoveMeCellIdentifier";

   UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:MoveMeCellIdentifier];

   if (cell==nil) {

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:MoveMeCellIdentifier];

        cell.showsReorderControl=YES;//不会真正显示重新排序控件,除非表进入编辑模式

    }

   NSUInteger row = [indexPathrow];

    cell.textLabel.text=[listobjectAtIndex:row];

   return  cell;

}



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

{

    //通过该方法表视图可以询问指定行是否可以被删除,或者是否可以将新行插入到指定位置。

    returnUITableViewCellEditingStyleNone;//表示我们不支持插入或删除任何行

}


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

{

    //每一行都将调用该方法,可以通过它禁止移动指定行。

    return YES;

}


-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

{

    //当移动一行时真正调用的方法两个参数分别只当被移动行和行的新位置。

   NSUInteger fromRow = [fromIndexPathrow];//需要移动行的新位置

   NSUInteger toRow = [toIndexPathrow];//检索新的位置

   id object =[listobjectAtIndex:fromRow];

    [list removeObjectAtIndex:fromRow];//数组中移除指定的对象

    [list insertObject:objectatIndex:toRow];//指定到新的位置

}

@end


可删除的行

#import "BIDSecondLevelViewController.h"

@interface BIDDeleteMeController :BIDSecondLevelViewController

@property (strong,nonatomic)NSMutableArray *list;

-(IBAction)toggleEdit:(id)sender;

@end


#import "BIDDeleteMeController.h"


@implementation BIDDeleteMeController


@synthesize list;


-(IBAction)toggleEdit:(id)sender

{

    [self.tableViewsetEditing:!self.tableView.editinganimated:YES];

    if (self.tableView.editing) {

        [self.navigationItem.rightBarButtonItemsetTitle:@"Done"];

    }

   else

        [self.navigationItem.rightBarButtonItemsetTitle:@"Delete"];

}


-(void)viewDidLoad

{

    [superviewDidLoad];

   if (list==nil) {

        NSString *path = [[NSBundlemainBundle]pathForResource:@"computers"ofType:@"plist"]; 

       NSMutableArray *array= [[NSMutableArrayalloc] initWithContentsOfFile:path];

       self.list=array;

    }

    UIBarButtonItem *editButton = [[UIBarButtonItemalloc]initWithTitle:@"Delete"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(toggleEdit:)];

    self.navigationItem .rightBarButtonItem= editButton;

}


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

{

   return [listcount];

}


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

{

   static NSString *DeleteMeCellIdentifier=@"DeleteMeCellIdentifier"

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCellIdentifier];

   if (cell==nil) {

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:DeleteMeCellIdentifier];

    }

   NSInteger row =[indexPath row];

    cell.textLabel.text=[self.listobjectAtIndex:row];

   return cell;

}


#pragma mark -

#pragma mark Table Data Source Methods

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

{

    //当用户完成一项编辑(插入或删除)操作时,表视图调用该方法

   NSInteger row =[indexPath row];

    [self.listremoveObjectAtIndex:row];

    [tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    

}

@end




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值