標籤:des style blog http 使用 io strong 資料
【1】拖動好介面
【2】設定協議,資料來源
【3】代碼
1.聲明可變數組,用來存放所有資料對象
@interface ViewController ()@property(nonatomic,strong)NSMutableArray *mydata;@end
2.初始化資料【建立30個對象資料】
- (void)viewDidLoad{ [super viewDidLoad]; self.mydata=[NSMutableArray array]; for(int i =0;i<30;i++) { NSString *str=[NSString stringWithFormat:@"it-%d",i]; [self.mydata addObject:str]; }}
3.設定返回行數
#pragma mark -返回行數-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.mydata.count;}
【刪除方法】
1.添加固定方法,顯示一個cell就調用該方法,最佳化效能
#pragma mark 每當有以個cell進入視野範圍內就會調用,返回當前這行顯示的cell-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 1. 用static 修飾的 局部變數,只會初始化一次 static NSString *[email protected]"Cell"; // 2. 拿到一個標識先去緩衝池中尋找對應的Cell UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; // 3. 如果緩衝池中沒有,才需要傳入一個標識建立新的Cell if(cell==nil) cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID]; // 4.覆蓋資料 cell.textLabel.text=self.mydata[indexPath.row]; [email protected]"哈哈哈"; return cell;}
2.這個方法是提交編輯時候調用【點擊Delete,就會調用下面的方法】
#pragma mark 提交編輯時調用// 一點擊刪除,就會調用這個方法/ 左滑動刪除-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"-------commit ------%d",indexPath.row); // 刪除資料 // 1. 更改資料(刪除本行資料) [self.mydata removeObjectAtIndex:indexPath.row];// 傳入一個位置,將這個位置上的資料刪除 // 2.重新整理資料 // [tableView reloadData];// 資料重新整理,全域刷 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}
3.調用代理方法中的監聽事件,【刪除】資訊內容的修改
[開啟編輯狀態]
self.tableView.editing=YES; |
// 編輯狀態 |
[self.tableView setEditing:YSE animated:YES]; |
// 帶有動畫效果 |
#pragma mark -代理方法
#pragma mark -監聽item的點擊#pragma mark 刪除-(IBAction)remove{ bool edt=self.tableView.editing; //取得現在編輯狀態是否開啟 //按一下開啟編輯狀態 // 沒有動畫效果 // self.tableView.editing=YES; // 編輯狀態 [self.tableView setEditing:!edt animated:YES]; // 帶有動畫效果}
【增加方法】
【1】.增加方法與刪除方法類似,先建立“+”按鈕的代理方法,連線
【2】.代碼
1.添加資料用的方法: [self.mydata insertObject:[添加的內容] atIndex:[添加到第幾行]];
2.重新整理資料: inSection 表示是組號,indexPathForRow表示的是行號
1)擷取到一個數組
NSIndexPath *newPath=[NSIndexPath indexPathForRow:[行號] inSection:[組別號]];
2)傳入一個數組
[tableView insertRowsAtIndexPaths:@[newPath] 【上面的數組】withRowAnimation:UITableViewRowAnimationTop【動畫效果】];
#pragma mark 提交編輯時調用(刪除和添加方法都會調用該方法)// 一點擊“+”就會調用這個方法-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ // 增加資料 // 1. 更改資料(刪除本行資料) [self.mydata insertObject:@"新添加資料"atIndex:indexPath.row+1]; // 2.重新整理資料 NSIndexPath *newPath=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]; [tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationTop];}
[tableView reloadRowsAtIndexPaths:<#(NSArray *)#>【指定行】 withRowAnimation:<#(UITableViewRowAnimation)#>] |
重新整理指定行的資料(個數不變) |
[tableView deselectRowAtIndexPath:<#(NSIndexPath *)#> 【指定行】 animated:<#(BOOL)#>【動畫效果】] |
刪除指定行(刪除後個數與資料個數保持一致) |
[tableView insertRowsAtIndexPaths:@[newPath]【指定行要求數組】 withRowAnimation:UITableViewRowAnimationTop]; |
插入新的行 |
3.加入add方法
-(IBAction)add{ // 取出當前的編輯狀態 bool edt=self.tableView.editing; // 設定調用方法,方便開啟編輯模式判斷是否是添加 self.tableView.tag=UITableViewCellEditingStyleInsert; // 開啟編輯模式 [self.tableView setEditing:!edt animated:YES];}
4.通過上面【add】方法與【remove】方法中設定的Tag,可以擷取調用移除還是增加方法。然後使用這個編輯模式。
#pragma mark -代理方法#pragma mark 當tableView開啟編輯模式就會調用-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ // 通過調用add 和remove方法中的tag變數,來確定是要添加還是刪除。 return tableView.tag;}
5.如果增加的話,最終會調用一下下面的方法,但是上面已經寫了
| 1234 |
#pragma mark 每當有以個cell進入視野範圍內就會調用,返回當前這行顯示的cell-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{} |
【排序模式】
1.增加代碼
sourceIndexPath :當前需要移動的行
destinationIndexPath :移動目標的行
#pragma mark 如果實現了這個方法,就有排序功能-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ // 當從某一行一動到另外一行時,會調用該方法。 NSLog(@"%d-->%d",sourceIndexPath.row,destinationIndexPath.row);// 這行可以說明調用步驟 // 如果直接使用這樣的方法,只是介面進行改變,資料沒有改變,違反了MVC所以要用以下方法 // 1.取出即將要刪除的資料 NSString *data =self.mydata[sourceIndexPath.row]; // 2.刪除要一動的那一行 [self.mydata removeObject:data]; // 插入之前刪除的資料到某一行 [self.mydata insertObject:data atIndex:destinationIndexPath.row];}
【總結】
1.增加\刪除
一、刪除\添加: 1.開啟編輯模式 [self.tableView setEditing:YES animated:YES]; 2.實現資料來源的某個方法 tableView:commitEditingStyle:forRowAtIndexPath: 3.下面方法的傳回值決定編輯模式是添加還是刪除 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
2.排序
二、排序: 實現下面的方法即可: tableView:moveRowAtIndexPath:toIndexPath:
3.重新整理介面
三、4個重新整理UI介面的方法 1.添加新的行 [tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; 2.刪除指定的行 [tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; 3.局部重新整理指定的行 [tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; 4.整體重新整理所有的行 [tableView reloadData];