现在大致就是画出这么一个界面,从而来实现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