滑動刪除指定行代碼以下:html
Controller.h文件atom
#import <UIKit/UIKit.h> @interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> @property (nonatomic, strong) UITableView *myTableView; @property(nonatomic,strong) NSMutableArray *arrayOfRows; @end Controller.m文件 #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
執行截圖:
spa