IOS學習之——Cell的添加、刪除以及移動

現在大致就是畫出這麼一個界面,從而來實現TableViewCell的刪除,新增、以及移動效果。

在這之前,需要創建一個類,並提供一個靜態方法DemoData獲取簡單的數據


#import "MyTableViewController.h"
#import "City.h"
@interface MyTableViewController ()@property(nonatomic,strong)NSMutableArray *allcities;
@end

@implementation MyTableViewController

//懶加載基礎數據
-(NSArray *)allcities {
    //創建一個城市,有兩個基礎屬性,城市名稱以及人口
    if (!_allcities) {
        _allcities = [[City demoData] mutableCopy];
    }
    return _allcities;
}


- (void)viewDidLoad {
    [super viewDidLoad];
   self.navigationItem.title = @"城市列表";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"編輯" style:UIBarButtonItemStylePlain target:self action:@selector(addCity:)];
    
//    //tableviewController 幫我們提供了一個可以修改 tableview 編輯狀態的 barButtonItem
//    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
}

//點中右上角按鍵  進入編輯狀態
-(void)addCity:(UIBarButtonItem*)sender {
    [self.tableView setEditing:!self.tableView.editing animated:YES];
//    isEditing editing的getter方法的 新名字
    sender.title = self.tableView.isEditing ? @"完成" : @"編輯";
}

//問一 改行是否可以編輯
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return NO;
    }
    return YES;
}

//問二 每行編輯是什麼樣式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        //返回 插入
        return UITableViewCellEditingStyleInsert;
    }
    //返回 刪除
    return UITableViewCellEditingStyleDelete;
}

//一答 點擊 某行 加號 或 減號 響應的動作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //將數據從數組中刪除
        [self.allcities removeObjectAtIndex:indexPath.row];
        //如果該行 編輯是 刪除, 從tableView中 把元素刪除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }else {
        //如果該行 編輯是 增加
        City *city = [[City alloc]init];
        city.name = @"深圳";
        city.population = 5000;
        //將元素添加到 點中行的下一行
        //將城市 添加到 數組中對應的位置
        [self.allcities insertObject:city atIndex:indexPath.row + 1];
        //刷新tableview
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

#pragma mark 移動的一問一答
//問一:當前行是否可以移動  (默認是不能移動的)
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
//答一:移動任意行後,如何響應
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    //1.先根據原始位置取出數去中該位置對應的 數據
    City *city = self.allcities[sourceIndexPath.row];
    //2.從原始位置把 數組中的元素 移除
    [self.allcities removeObjectAtIndex:sourceIndexPath.row];
    //3.將數據 插入到數組中 新的位置
    [self.allcities insertObject:city atIndex:destinationIndexPath.row];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allcities.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"------------------------------");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"reuseIdentifier"];
    }
    City *city = self.allcities[indexPath.row];
    cell.textLabel.text = city.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",city.population];
    
    
    return cell;
}

@end





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章