IOS學習之路六(UITableView滑動刪除指定行)


滑動刪除指定行代碼如下:


Controller.h文件

 

#import <UIKit/UIKit.h>

@interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property(nonatomic,strong) NSMutableArray *arrayOfRows;
@end


 

Controller.m文件

 

//
//  TableViewController.m
//  UITableViewDemo
//
//  Created by WildCat on 13-8-6.
//  Copyright (c) 2013年 wildcat. All rights reserved.
//

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController
@synthesize myTableView;
@synthesize arrayOfRows;


- (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:UITableViewStyleGrouped];
    //設置列表樣式為簡單的樣式 還有一個樣式為UITableViewStyleGrouped為分組模式   UITableViewStylePlain為普通的樣式
    self.myTableView.delegate = self;//設置代理為自身
    self.myTableView.dataSource = self;//設置數據源為自身
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    //確保TablView能夠正確的調整大小
    arrayOfRows = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d", nil];//初始化表格數據
    [self.view addSubview:myTableView];
    
}
//設置每行的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat result = 20.0f;
    if ([tableView isEqual:self.myTableView]) {
       
        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.arrayOfRows 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 = [self.arrayOfRows 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.arrayOfRows count]) {
            [self.arrayOfRows removeObjectAtIndex:indexPath.row];//移除數據源的數據
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的數據
        }
    }
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
   // self.myTableView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end







執行截圖:

 







 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM