对于app应用来说,使用列表的形式展现数据非UITableView莫属.在熟练掌握了用UITableView展示数据以后,是不是也遇到了需要删除数据的需求?是不是觉得在一行数据上划动一下,然后出现一个删除按钮很酷?废话少说,直奔正题,就由笔者来向您展示一下这个功能的实现是多么容易.
先前的准备工作:
第一步,准备好数据源.
 
  1. #import <UIKit/UIKit.h>
  2. @interface UITableCellSwapDeleteViewController : UIViewController <UITableViewDelegate>{
  3. IBOutlet UITableView *testTableView;
  4. NSMutableArray *dataArray;
  5. }
  6. @property (nonatomic, retain) UITableView *testTableView;
  7. @property (nonatomic, retain) NSMutableArray *dataArray;
  8. @end
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
  12. }
这里笔者定义了并实现了一个一维的可变数组.为什么要用可变数组呢?因为我们要删除里面的数据呀.
 
第二步,展示数据.
 
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  2. // Return the number of sections.
  3. return 1;
  4. }
  5. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  6. // Return the number of rows in the section.
  7. return [dataArray count];
  8. }
  9. // Customize the appearance of table view cells.
  10. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  11. static NSString *CellIdentifier = @"Cell";
  12. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  13. if (cell == nil) {
  14. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  15. }
  16. // Configure the cell...
  17. cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
  18. return cell;
  19. }
通过实现上面三个代理方法向UITableView中添加了数据.

 
通过上面两步就实现了数据展示工作,接下就实现关键的数据删除了.
 
 
  1. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  2. return YES;
  3. }
  4. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  5. if (editingStyle == UITableViewCellEditingStyleDelete) {
  6. [dataArray removeObjectAtIndex:indexPath.row];
  7. // Delete the row from the data source.
  8. [testTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  9. }
  10. else if (editingStyle == UITableViewCellEditingStyleInsert) {
  11. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
  12. }
  13. }
 启用上面两个代理,并增加数据删除操作:
[dataArray removeObjectAtIndex:indexPath.row];
 在一条数据上向右划动一下.

 点Delete.

 是不是就成功删除了一条数据呢?
 按理说故事讲到这里也就讲完了.但是笔者想延伸一下.注意看图二划动以后的"Delete",你有没有想把这个东东改掉的冲动呢?比如改成:下载?其实很简单,其实下面这个代理方法:
 
  1. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
  2. return @"下载";
  3. }
再划动一下,是不是变了呢?

相关文章:

UITableView多选删除,类似mail中的多选删除效果

具体代码见附件

本文出自 “rainbird” 博客,请务必保留此出处http://rainbird.blog.51cto.com/211214/634587

最新文章

  1. css3盒模型
  2. Microsoft Dynamics CRM 2013 Js Odata 查询
  3. flash小游戏在Kongregate上线——BasketBall Master(篮球大师)
  4. 用SQL语句将数据表中的数据保存为JSON格式
  5. SqlServer--delete、truncate 、Drop删除表的区别
  6. 【MySQL】PREPARE 的应用
  7. POJ 1556 The Doors(线段交+最短路)
  8. 【iOS技术】Xcode+GitHub远程代码托管(GIT, SVN)
  9. Ubuntu下SVN命令行递归加入文件夹文件(免去一个一个的加入 --force)
  10. .c和.h档
  11. 谈JS中的作用域链与原型链(1)
  12. Go指针
  13. LeetCode &amp; Q414-Third Maximum Number-Easy
  14. SSH通过密钥登陆
  15. Kubernetes学习之路(26)之kubeasz+ansible部署集群
  16. web3.js编译Solidity,发布,调用全部流程(手把手教程)
  17. Unix环境高级编程:文件 IO 原子性 与 状态 共享
  18. sysbench压测mysql基本步骤
  19. 通配符证书导致 Outlook Anywhere 的客户端连接问题
  20. UVALive - 6837 Kruskal+一点性质(暴力枚举)

热门文章

  1. 基于jquery带时间轴的图片轮播切换代码
  2. 通过google浏览器的开发者工具修改cookie值
  3. js获取checkbox值的方法
  4. xml选择节点方法
  5. Android中使用SoundPool来播放音频
  6. Elasticsearch5.X IN Windows 10 系列文章(5)
  7. 上手并过渡到PHP7(1)——基于Homestead的PHP7和XDdebug环境
  8. SQL计算表的列数
  9. MySQL多表数据记录查询详解
  10. bt开源的客户端——xbt client