ios之UITableViewController(二) tableView的編輯模式,iostableview刪除行

來源:互聯網
上載者:User

ios之UITableViewController(二) tableView的編輯模式,iostableview刪除行

tableView的編輯模式

   表視圖可以進入編輯模式,當進入編輯模式就可以進行刪除、插入、移動單元等操作

  :

  

   讓表視圖進入編輯模式,進入編輯模式的方法有兩種,一種是使用導覽列的edit 

     按鈕,另一種是設定tableView的editing屬性進入編輯模式。

     最後通過實現UITableViewDataSource協議的方法實現儲存格的刪除、插入和移動

 

1,在viewDidLoad方法裡面指定導覽列的右按鈕為edit按鈕

  self.navigationItem.rightBarButtonItem =self.editButtonItem;

 

 2,另一種進入編輯模式的方式是修改tableView的editing屬性

  該屬性是一個BOOL類型,預設值是NO,這裡給導覽列添加一個左按鈕,通過點擊左按鈕修改editing屬性的值

   進入和退出編輯模式

 

- (void)editTableView:(UIBarButtonItem *)button

 {

  //修改editing屬性的值,進入或退出編輯模式

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

   if(self.tableView.editing){

   button.title = @"完成";

   }

  else{

   button.title = @"編輯";

     }

}

 

 

實現刪除和插入行

  兩方法一響應

方法一:那些行進入編輯模式

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

{

    return YES;

}

 

方法二:進入編輯模式的cell是刪除還是增加

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

{

    return UITableViewCellEditingStyleDelete;

}

 

注意:這個方法裡一般返回兩種類型,還有一中預設類型

刪除:UITableViewCellEditingStyleDelete

增加:UITableViewCellEditingStyleInsert

 

響應:點擊當點擊delete後執行的刪除過程

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

{

  //刪除資料來源裡的資料

  [self.array removeObjectAtIndex:indexPath.row];

  //再刪除tableView中對應的行

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

注意:先除資料來源裡的資料,刪除tableView中對應的行

 

 

實現移動行

  當tableView進入編輯模式之後,預設每一行都是可以移動,每一行尾部有一個表徵圖

  為三行灰色橫線的按鈕,就是表示該行可以移動

 一方法一響應

 

方法一:那些cell可以移動

-(BOOL)tableView:(UITableView *)tableView

canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

 

響應:移動的具體操作

- (void)tableView:(UITableView *)tableView

moveRowAtIndexPath:(NSIndexPath *)fromIndexPath

      toIndexPath:(NSIndexPath *)toIndexPath

{

    TRStudent *stu=self.array[fromIndexPath.row];

    [self.array removeObjectAtIndex:fromIndexPath.row];

    [self.array insertObject:stuatIndex:toIndexPath.row];

}

 

 

案例

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

 

AppDelegate.m

#import "AppDelegate.h"

#import"TRStudent.h"

#import "TRTableViewController.h"

@implementation AppDelegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    TRTableViewController *tr1=[[TRTableViewController alloc] initWithNibName:@"TRTableViewController" bundle:nil];

    UINavigationController *navi=[[UINavigationController alloc] initWithRootViewController:tr1];

    self.window.rootViewController=navi;

    [self.window makeKeyAndVisible];

    return YES;

}

@end

 

TRTableViewController.h

#import <UIKit/UIKit.h>

@interface TRTableViewController : UITableViewController

@property(nonatomic,strong) NSMutableArray *array;

@end

 

TRTableViewController.m

#import "TRTableViewController.h"

#import "TRStudent.h"

@interface TRTableViewController ()

@end

@implementation TRTableViewController

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

{

    return self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.array=[TRStudent getarray];

    self.navigationItem.rightBarButtonItem=self.editButtonItem;

  }

 

 

//有多少分區

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

 

//有多少個cell單元

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

{

    return self.array.count;

}

 

//cell單元

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

{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(cell==nil)

    {

        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

    }

    TRStudent *stu=self.array[indexPath.row];

    cell.textLabel.text=stu.name;

    return cell;

}

 

 

//那些行進入編輯模式,根據你的需求自行設定,我這裡設定的全部

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

{

    

    return YES;

}

 

//進入編輯模式的cell是刪除還是增加

//自行設定,我這裡設定的是最後一個cell單元是增加,其他是刪除

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView

          editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(indexPath.row==self.array.count-1)

        return UITableViewCellEditingStyleInsert;

    return UITableViewCellEditingStyleDelete;

}

 

//點擊當點擊delete後執行的刪除增加過程

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

{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        

            [self.array removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        

        TRStudent *str2=[[TRStudent alloc] init];

        str2.name=@"qwer";

        str2.name=@"124456";

        [self.array addObject:str2];

        NSIndexPath *insetindexpath=[NSIndexPath indexPathForRow:self.array.count-1 inSection:0];

        [tableView insertRowsAtIndexPaths:@[insetindexpath] withRowAnimation:UITableViewRowAnimationAutomatic];

        

    }

}

//那些cell可移動

-(BOOL)tableView:(UITableView *)tableView

canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

 

//移動

- (void)tableView:(UITableView *)tableView

moveRowAtIndexPath:(NSIndexPath *)fromIndexPath

      toIndexPath:(NSIndexPath *)toIndexPath

{

    TRStudent *stu=self.array[fromIndexPath.row];

    [self.array removeObjectAtIndex:fromIndexPath.row];

    [self.array insertObject:stuatIndex:toIndexPath.row];

     }

 

@end

 

 

 

TRStudent.h

#import <Foundation/Foundation.h>

 @interface TRStudent : NSObject

@property(nonatomic,strong) NSString *name;

@property(nonatomic,strong) NSString *phone;

+(NSMutableArray *)getarray;

@end

 

TRStudent.m

#import "TRStudent.h"

@implementation TRStudent

+(NSMutableArray *)getarray

{

    TRStudent *stu1=[[TRStudent alloc] init];

    stu1.name=@"q";

    stu1.phone=@"12345";

    TRStudent *stu2=[[TRStudent alloc] init];

    stu2.name=@"qqw";

    stu2.phone=@"12345";

    TRStudent *stu3=[[TRStudent alloc] init];

    stu3.name=@"sdsq";

    stu3.phone=@"12345";

    NSMutableArray *mut=[[NSMutableArray alloc] init];

    [mut addObject:stu1];

    [mut addObject:stu2];

    [mut addObject:stu3];

    return mut;

}

@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.