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
新聞熱點
疑難解答