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




發佈了102 篇原創文章 · 獲贊 35 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章