標籤:des style io color ar os sp for strong
tableView編輯
1、讓tableView處於編輯狀態
2、指定tableView哪些行可以編輯
3.指定tableView編輯的樣式(添加、刪除)
4、編輯完成(先操作資料源,再修改UI)
tableView移動
1、讓tableView處於編輯狀態
2、指定tableView哪些行可以移動
3.移動完成
4.監測移動過程,實現限制跨區移動
?論編輯還是移動,都先讓tableView進入編輯狀態。 編輯結束或者移動結束,要先修改數組或字典中的資料,在更改UI。
UITableViewController是封裝好了各種delegate和datasource,能 提高我們開發速度
<span style="font-size:18px;color:#330033;">#import "RootViewController.h"#import "RootView.h"@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>{ UITableViewCellEditingStyle _style;}@property(nonatomic,strong)RootView *myview;@property(nonatomic,strong)NSMutableArray *groupArray;@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.myview = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; } return self;}-(void)loadView{ self.view = self.myview;}- (void)viewDidLoad{ [super viewDidLoad]; self.groupArray = [NSMutableArray array]; //設定代理 self.myview.TableView.dataSource = self; //設定代理 self.myview.TableView.delegate = self; [self addAction]; }-(void)addAction{ //建立一個barbutton按鈕,(刪除按鈕) UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] initWithTitle:@"刪除" style:(UIBarButtonItemStyleDone) target:self action:@selector(daleteButton:)]; self.navigationItem.leftBarButtonItem = deleteButton; UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(addButton:)]; self.navigationItem.rightBarButtonItem = addButton; //建立一個假資料 for(int i = 0;i < 3;i++) { NSMutableArray *tempArray = [NSMutableArray array]; for(int j = 0;j < 5;j++) { [tempArray addObject:[NSString stringWithFormat:@"第%d組,第%d個人",i,j]]; } [self.groupArray addObject:tempArray]; }// NSLog(@"%@",self.groupArray[0]); }//編輯狀態//刪除按鈕-(void)daleteButton:(UIBarButtonItem *)sender{ //當點擊"刪除"按鈕的時候,觸發事件,給他一個編輯狀態(刪除狀態) _style = UITableViewCellEditingStyleDelete; //一個BOOL 接收此時的編輯狀態 BOOL flag = self.myview.TableView.editing; //設定編輯狀態 [self.myview.TableView setEditing:!flag animated:YES]; }//添加按鈕-(void)addButton:(UIBarButtonItem *)sender{ _style = UITableViewCellEditingStyleInsert; BOOL flag = self.myview.TableView.editing; [self.myview.TableView setEditing:!flag animated:YES]; }/*-----------------------UITableViewDataSource-----------------------------*///下面的方法都是遵循UITableViewDataSource協議的,所以先在viewDidLoad先去設定代理//設定幾個分區- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.groupArray.count;}//設定一個分區裡面有幾行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.groupArray[section] count];}//設定cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //標識 static NSString *cell_id = @"cell_id"; //建立cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id]; //判斷在重用池中有沒有cell,如果沒有就建立 if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cell_id]; } //給cell設定標題 cell.textLabel.text = self.groupArray[indexPath.section][indexPath.row]; //給cell設定副標題 cell.detailTextLabel.text = @"我中獎了"; return cell;}//指定哪些行可以被編輯-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ //例如讓單行處於被編輯狀態 if(indexPath.row % 2) { return NO; } return YES;}//編輯樣式(添加,刪除)-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return _style;}//編輯完成-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //當編輯狀態為添加狀態的時候 if(editingStyle == UITableViewCellEditingStyleDelete) { //刪除(確定哪個分區的哪一行) [self.groupArray[indexPath.section]removeObjectAtIndex:indexPath.row]; //重新整理資料 [tableView reloadData]; //重新整理資料動畫// [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { NSString *string = @"測試資料"; //修改資料來源(實現添加) [self.groupArray[indexPath.section]insertObject:string atIndex:indexPath.row+1]; //修改UI // [tableView reloadData]; // [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationTop)]; //添加資料到顯示到添加行下面 NSIndexPath *indes = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]; //重新整理資料帶動畫效果 [tableView insertRowsAtIndexPaths:@[indes] withRowAnimation:(UITableViewRowAnimationTop)]; } }/*-------------------------------tableView移動----------------------------------------*///指定哪些被移動-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}//移動完成-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ //記錄 id obj = self.groupArray[sourceIndexPath.section][sourceIndexPath.row]; //移除 [self.groupArray[sourceIndexPath.section]removeObjectAtIndex:sourceIndexPath.row]; //添加 [self.groupArray[destinationIndexPath.section]insertObject:obj atIndex:destinationIndexPath.row]; //更新資料來源 [tableView reloadData]; }//限制移動地區-(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{ //先判斷源和目的的分組是否相同, if (sourceIndexPath.section != proposedDestinationIndexPath.section) { //如果不同,返回源的起始位置 return sourceIndexPath; } else { //如果相同,返回目的的位置 return proposedDestinationIndexPath; }}</span>
ios UITableView表視圖(2)