IOS開發(25)之刪除UITableView的Cell

來源:互聯網
上載者:User

1 前言
今天我們來學習一下怎樣從UITableView中滑動刪除Cell資料。

2 代碼執行個體
ZYViewController.h


[plain]
#import <UIKit/UIKit.h> 
 
@interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//添加代理 
 
@property(nonatomic,strong) UITableView *myTableView; 
@property(nonatomic,strong) NSMutableArray *arryOfRows; 
 
@end 

#import <UIKit/UIKit.h>

@interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>//添加代理

@property(nonatomic,strong) UITableView *myTableView;
@property(nonatomic,strong) NSMutableArray *arryOfRows;

@end
ZYViewController.m

 

[plain]
@synthesize myTableView; 
@synthesize arryOfRows; 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//設定列表樣式為簡單的樣式 還有一個樣式為UITableViewStyleGrouped為分組模式   UITableViewStylePlain為普通的樣式 
    self.myTableView.delegate = self;//設定代理為自身 
    myTableView.dataSource = self;//設定資料來源為自身 
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//確保TablView能夠正確的調整大小 
    arryOfRows = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c", nil];//初始化表格式資料 
    [self.view addSubview:myTableView]; 
     

//設定每行的高度 
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    CGFloat result = 20.0f; 
    if ([tableView isEqual:self.myTableView]) { 
//        result = 40.0f; 
        result = 80.0f; 
    } 
    return result; 

////允許資料來源告知必須載入到Table View中的表的Section數。 
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
//    NSInteger result = 0; 
//    if([tableView isEqual:myTableView]){ 
//        result = 3;//一共三個section 
//    } 
//    return result; 
//} 
//設定每個Section呈現多少行 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [self.arryOfRows count]; 

//每行對應的資料 
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *result = nil; 
    if ([tableView isEqual:myTableView]) { 
        static NSString *tableViewCellIdentifier = @"MyCells";//設定Cell標識 
        result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通過標示符返回一個可重用的表視圖儲存格對象 
        if (result == nil) { 
            result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一個表格儲存格樣式和重用的標識符,並將它返回給調用者。 
        } 
        //indexPath.section 表示section的索引 indexPath.row表示行數的索引 
        result.textLabel.text = [arryOfRows objectAtIndex:indexPath.row]; 
    } 
    return result; 

 
//點擊某一行時候觸發的事件 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if ([tableView isEqual:myTableView]) { 
        NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]); 
    } 

//要求委託方的編輯風格在表視圖的一個特定的位置。 
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//預設沒有編輯風格 
    if ([tableView isEqual:myTableView]) { 
        result = UITableViewCellEditingStyleDelete;//設定編輯風格為刪除風格 
    } 
    return result; 

 
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{//設定是否顯示一個可編輯檢視的視圖控制器。 
    [super setEditing:editing animated:animated]; 
    [self.myTableView setEditing:editing animated:animated];//切換接收者的進入和退出編輯模式。 

 
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//請求資料來源提交的插入或刪除指定行接收者。 
    if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果編輯樣式為刪除樣式 
        if (indexPath.row<[self.arryOfRows count]) { 
            [self.arryOfRows removeObjectAtIndex:indexPath.row];//移除資料來源的資料 
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的資料 
        } 
    } 

@synthesize myTableView;
@synthesize arryOfRows;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//設定列表樣式為簡單的樣式 還有一個樣式為UITableViewStyleGrouped為分組模式   UITableViewStylePlain為普通的樣式
    self.myTableView.delegate = self;//設定代理為自身
    myTableView.dataSource = self;//設定資料來源為自身
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//確保TablView能夠正確的調整大小
    arryOfRows = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c", nil];//初始化表格式資料
    [self.view addSubview:myTableView];
   
}
//設定每行的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat result = 20.0f;
    if ([tableView isEqual:self.myTableView]) {
//        result = 40.0f;
        result = 80.0f;
    }
    return result;
}
////允許資料來源告知必須載入到Table View中的表的Section數。
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    NSInteger result = 0;
//    if([tableView isEqual:myTableView]){
//        result = 3;//一共三個section
//    }
//    return result;
//}
//設定每個Section呈現多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arryOfRows count];
}
//每行對應的資料
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *result = nil;
    if ([tableView isEqual:myTableView]) {
        static NSString *tableViewCellIdentifier = @"MyCells";//設定Cell標識
        result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通過標示符返回一個可重用的表視圖儲存格對象
        if (result == nil) {
            result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一個表格儲存格樣式和重用的標識符,並將它返回給調用者。
        }
        //indexPath.section 表示section的索引 indexPath.row表示行數的索引
        result.textLabel.text = [arryOfRows objectAtIndex:indexPath.row];
    }
    return result;
}

//點擊某一行時候觸發的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:myTableView]) {
        NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]);
    }
}
//要求委託方的編輯風格在表視圖的一個特定的位置。
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//預設沒有編輯風格
    if ([tableView isEqual:myTableView]) {
        result = UITableViewCellEditingStyleDelete;//設定編輯風格為刪除風格
    }
    return result;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{//設定是否顯示一個可編輯檢視的視圖控制器。
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];//切換接收者的進入和退出編輯模式。
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//請求資料來源提交的插入或刪除指定行接收者。
    if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果編輯樣式為刪除樣式
        if (indexPath.row<[self.arryOfRows count]) {
            [self.arryOfRows removeObjectAtIndex:indexPath.row];//移除資料來源的資料
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的資料
        }
    }
}
運行結果:

 
 


滑動後效果:

 
 


刪除後效果:

 


 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.