IOS学习之——Cell的添加、删除以及移动
来源:互联网 发布:租车软件 编辑:程序博客网 时间:2024/04/29 06:12
现在大致就是画出这么一个界面,从而来实现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
0 0
- IOS学习之——Cell的添加、删除以及移动
- cell 的选择, 移动 , 删除, 移动, 添加
- iOS开发:UITableView编辑——cell的删除和移动
- IOS学习之——自定义的Cell
- IOS学习之——自定义的Cell (代码)
- ios UITableView的移动、添加和删除
- ios中tableview的移动添加删除
- Snail—UI学习之表视图TableView单行添加、删除和移动
- IOS学习之——静态单元格 static cell
- iOS开发:通讯录之联系人添加、移动、删除
- IOS开发(25)之删除UITableView的Cell
- UIday1002:UITableView 2 用UITableView实现通讯录cell简单的数据移动、删除、添加功能
- UICollectionview实现自定义cell的移动删除
- iOS_UITableView 编辑(cell的插入, 删除, 移动)
- ios之UITableView批量选中cell并实现删除cell的简单方法
- iOS table简单实现增加移动和删除功能与自定义cell的实现
- iOS之如何处理TableView cell内容不一致以及cell复用重叠的问题
- iOS—TableView之Cell的Style属性
- git学习笔记
- freescale imx6 linux gpio中断驱动
- 大数据基础
- 在centos6.5 64bit环境下安装最新版本的git
- 【GDOI模拟】最大异或和
- IOS学习之——Cell的添加、删除以及移动
- BestCoder Round #77 (div.2) C India and China Origins
- diffie-hellman-group1-sha1
- 40个Java集合类面试题和答案
- 《战拖有术》读书笔记(VI)
- 内存映射(Linux设备驱动程序)
- MongoDB总结(三):mongoDB的主从复制、副本集、分布式部署
- Qt5.3.2移植到到开发板上触摸屏不可用的问题,望指教.
- 我对Android的认识