国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ios之UITableViewController(二)tableView的編輯模式

2019-11-14 18:18:12
字體:
來源:轉載
供稿:網友

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>

@

@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

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汝阳县| 淮安市| 屯门区| 葫芦岛市| 鹤山市| 荔波县| 岳阳县| 雷波县| 神池县| 阳新县| 台安县| 桂平市| 建昌县| 四子王旗| 宿州市| 渝北区| 西贡区| 淳安县| 大安市| 大埔县| 长春市| 武隆县| 阿拉善右旗| 时尚| 临沂市| 鸡东县| 应城市| 石棉县| 亚东县| 永安市| 宣城市| 宜兰市| 盐山县| 元谋县| 乐平市| 辽宁省| 响水县| 灵武市| 鹤庆县| 延长县| 磐石市|