默认的,如果表视图支持编辑,那用户可以通过两种方式来删除某些行,其一为单击左侧的红色按钮后行右侧显示“Delete”按钮,其二为在单元行上的手指向左滑动,“Delete”按钮也会出现供用户单击。无论哪种方式,只要用户单击了“Delete”,开发者需要确保数据源的更新和处理界面上单元行的消失。

根据这个工作流程开始撰写代码。新建一个继承自上节HBCustomLayoutViewController的子类名为HBDeleteViewController,由于需要一个按钮来触发编辑状态,不如将按钮置于导航栏右侧,设计成点击一次开启编辑模式,再次点击则关闭编辑模式。这样头文件的内容如下:

 #import "HBCustomLayoutViewController.h"

 @interface HBDeleteViewController : HBCustomLayoutViewController

 @property (nonatomic,retain)UIBarButtonItem *editItem;
@property (nonatomic,retain)UIBarButtonItem *doneItem;

在类实现中,数据源已经由HBCustomLayoutViewController制作好,所以只需要对导航栏的按钮进行配置。

 @implementation HBDeleteViewController
@synthesize editItem=_editItem;
@synthesize doneItem=_doneItem; -(void)initUI
{
[super initUI]; //开启编辑模式按钮
_editItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(actBeginEdit:)]; //关闭编辑模式按钮
_doneItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actEndEdit:)]; self.navigationItem.rightBarButtonItem=_editItem;
} #pragma marl-
#pragma mark Action
-(IBAction)actBeginEdit:(id)sender
{
//开启编辑模式
[self.tableView setEditing:YES animated:YES];
self.navigationItem.rightBarButtonItem=_doneItem;
} -(IBAction)actEndEdit:(id)sender
{
//关闭编辑模式
[self.tableView setEditing:NO animated:YES];
self.navigationItem.rightBarButtonItem=_editItem;
}

随后依据删除的工作流程,完成数据源回调函数和代理回调函数的相应实现

#pragma mark
#pragma mark Table View data source
//setEditing:animated:后被调用
//询问具体Cell是不是支持编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
} -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *arrNewDatasource=[NSMutableArray arrayWithArray:self.datasource]; //cell上的“delete”按钮点击
if (editingStyle == UITableViewCellEditingStyleDelete) {
if(indexPath.row>=arrNewDatasource.count)
{
return;
} //删除
[arrNewDatasource removeObjectAtIndex:indexPath.row];
//更新datasource
_datasource=[[NSArray alloc]initWithArray:arrNewDatasource]; //更新界面
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
} #pragma mark
#pragma mark TableView Delegate
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//return UITableViewCellEditingStyleDelete; //只有编辑状态时,才有删除功能
//由于手指左划显示“Delete”按钮并不处于编辑状态,所以会被屏蔽掉
if(self.tableView.editing)
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone; }

运行代码,其显示效果如下:

IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)的更多相关文章

  1. IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)

    表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的 下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码. 显示下过如下: #pragma mark #pragma mark Tabl ...

  2. iOS 表视图(UITableVIew)的使用方法(1)表视图的示例

    表视图继承自UIScrollView,所以有着大多UIScrollView的操作特性,诸如手指控制内容的滚动,内容视图到顶端或者低端时的自动反弹等.配合UINavigationController的导 ...

  3. IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)

    在表视图的删除操作中,每次只能够对其中一个单元进行删除,如果想要同时删除多条记录,不得不挨个地进行标准的删除操作 所以如果能够实现多选的机制,无论是删除还是其他功能的嫁接,都会变得更加方便 当UITa ...

  4. IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)

    除了每个单元行左边的删除和新增图标,UITableView还支持在单元行的右侧显示一个供用户拖拉调整排序位置的控件. 不过如果要显示此控件,UITableView的数据源需要实现以下的方法. -(vo ...

  5. IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示

    当数据量特别大时,简单地以role进行分段,对实际查找的效率提升并不大.就像上一节开头所说,开发者可以根据球员名字的首字母进行分段,且分成26段.由于段数较多,可以使用UITableView的索引机制 ...

  6. IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示

    我们可以采用名字分段法,名字分段会在之后的小节中显示,这是转而使用球员的角色分段发,以最直接的入手点讲解好UITableView的分段使用方法.本节示例时基于上节的SimpleTableViewCon ...

  7. IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元

    UITableViewCell的自定义往往需要自建一个UITableViewCell的子类后进行作业.开发者可以选择通过xib或者直接在UITableViewCell的布局中进行UITableView ...

  8. iOS开发UITableView基本使用方法总结

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  9. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

随机推荐

  1. iOS-BLE蓝牙开发

    Demo地址:WEBlueToothManager 在写这个博客之前,空余时间抽看了近一个月的文档和Demo,系统给的解释很详细,接口也比较实用,唯独有一点,对于设备 的唯一标示,网上众说纷纭,在这里 ...

  2. 算法精解(C语言描述) 第5章 读书笔记

    第5章 5.1 单链表 /* -------------------------------- list.h -------------------------------- */ #ifndef L ...

  3. BZOJ 3774: 最优选择( 最小割 )

    最小割...二分染色然后把颜色不同的点的源汇反过来..然后就可以做了. 某个点(x,y): S->Id(x,y)(回报), Id(x,y)->T(代价), Id(i,j)&& ...

  4. leetcode Climbing Stairs python

    class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...

  5. JAVA之GUI编程ACTION事件

    package GUI; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java. ...

  6. codeforces 632E. Thief in a Shop fft

    题目链接 E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input stan ...

  7. linux之sed

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  8. Changing the Auto-Logout Timeout in SSH

    SSH: We can set a timeout interval for ssh client who are idle or in inactive state. As soon as the ...

  9. easyui-layout中的收缩层无法显示标题问题解决

    先看问题描述效果图片: 如上,我的查询条件是放在layout下面的一个可收缩层中,初始是收缩的,title显示不出来的话对使用者很不方便,代码如下: <div id="__MODULE ...

  10. nginx access log logrotate配置

    /home/deployuser/deploy/nginx/temp/logs/home.access.log {   size 100M   rotate 100    nocompress   d ...