【UIKit】UITableView.07 編輯模式

【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.設置返回行數atom

#pragma mark -返回行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mydata.count;
}

 


 

【刪除方法】
url

1.添加固定方法,顯示一個cell就調用該方法,優化性能spa

#pragma mark 每當有以個cell進入視野範圍內就會調用,返回當前這行顯示的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1. 用static 修飾的 局部變量,只會初始化一次
    static NSString *ID=@"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];
    cell.detailTextLabel.text=@"哈哈哈";
    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.調用代理方法中的監聽事件,【刪除】信息內容的修改code

[開啓編輯狀態]

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.若是增長的話,最終會調用一下下面的方法,可是上面已經寫了

1
2
3
4
#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、刪除\添加:
 1.開啓編輯模式
 [self.tableView setEditing:YES animated:YES];
 
 2.實現數據源的某個方法
 tableView:commitEditingStyle:forRowAtIndexPath:
 
 3.下面方法的返回值決定編輯模式是添加仍是刪除
 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

2.排序

 2、排序:
 實現下面的方法便可:
 tableView:moveRowAtIndexPath:toIndexPath:

3.刷新界面

3、4個刷新UI界面的方法
 1.添加新的行
 [tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];
 
 2.刪除指定的行
 [tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];
 
 3.局部刷新指定的行
 [tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];
 
 4.總體刷新全部的行
 [tableView reloadData];
相關文章
相關標籤/搜索