IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)
除了每个单元行左边的删除和新增图标,UITableView还支持在单元行的右侧显示一个供用户拖拉调整排序位置的控件。
不过如果要显示此控件,UITableView的数据源需要实现以下的方法。
-(void)tableView:(UITableView *)tableview moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
这样当UITableView进入编辑模式时,右侧的位置调整控件会依次询问每个单元行是否需要显示,可以实现数据源的以下方法来配置每一个单元右侧此控件的显示与否。
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
另外,如果开发者想支持排序功能,有必要在编辑模式时将诸如删除和新增的功能给屏蔽掉已达到更好的用户体验效果。
排序的新类中,对于非UITableView数据源和代理函数实现的地方,大致和HBDeleteViewController一样,只是需要改动导航栏的按钮外观而已,代码如下:
-(void)initUI
{
[super initUI]; //导航栏右侧的排序按钮
UIButton *aOrderButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 23.0f, 15.0f)]; [aOrderButton addTarget:self action:@selector(actBeginOrder:) forControlEvents:UIControlEventTouchUpInside];
[aOrderButton setImage:[UIImage imageNamed:@"reorder"] forState:UIControlStateNormal]; _orderItem=[[UIBarButtonItem alloc]initWithCustomView:aOrderButton];
//关闭编辑模式按钮
_doneItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actEndOrder:)]; self.navigationItem.rightBarButtonItem=_orderItem;
} #pragma marl-
#pragma mark Action
-(IBAction)actBeginOrder:(id)sender
{
//开启编辑模式
[self.tableView setEditing:YES animated:YES];
self.navigationItem.rightBarButtonItem=_doneItem;
} -(IBAction)actEndOrder:(id)sender
{
//关闭编辑模式
[self.tableView setEditing:NO animated:YES];
self.navigationItem.rightBarButtonItem=_orderItem;
}
随后做更关键的事:数据源和代理方法的实现,代码如下:
#pragma mark
#pragma mark Table View data source
//setEditing:animated:后被调用
//询问具体Cell是不是支持编辑 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
//假设需求:教练不能移动
if(indexPath.row == )
{
return NO;
}
return YES;
} -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
} -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSMutableArray *arrNewDatasource = [NSMutableArray arrayWithArray:self.datasource]; HBPlayerInfo *aPlayer = [self.datasource objectAtIndex:sourceIndexPath.row]; //更新数据源
[arrNewDatasource removeObjectAtIndex:sourceIndexPath.row];
[arrNewDatasource insertObject:aPlayer atIndex:destinationIndexPath.row]; _datasource=[[NSArray alloc]initWithArray:arrNewDatasource];
} -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
//更新UI
//拖动时,目的地位置的承认与否
//如果不承认,可以自己制作一个有效地目的地位置NSIndexPath,返回出去
if (proposedDestinationIndexPath.row==) {
//要超过首行?不行!
return [NSIndexPath indexPathForRow: inSection:proposedDestinationIndexPath.section];
}
return proposedDestinationIndexPath;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"InfoTableViewCellId";
HBCustomCell *cell =(HBCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil)
{
NSArray *arrNib=[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:self options:nil];
if(arrNib)
{
//第一个元素就是需要的UITableViewCell
cell=(HBCustomCell *)[arrNib objectAtIndex:];
}
} UIImage *photo = nil;
HBPlayerInfo *onePlayer=[self.datasource objectAtIndex:indexPath.row];
if(onePlayer)
{
cell.playerName.text=onePlayer.name;
cell.playerName.font=[UIFont fontWithName:@"Helvetica" size:16.0f];
cell.playerRole.text=onePlayer.role;
cell.playerRole.font=[UIFont fontWithName:@"Helvetica" size:12.0f];
cell.playerNumber.text=[NSString stringWithFormat:@"No.%@",onePlayer.number]; //插入时,更新界面的方法insertRowsAtIndexPaths会调用cellForRowAtIndexPath询问具体的UITableViewCell
//所以这里考虑为插入的元素准备的空头像
photo=[UIImage imageNamed:@"gaolin.jpeg"];
if(!photo)
{
photo=[UIImage imageNamed:@"empty"];
}
cell.playerPhoto.image=photo;
cell.showsReorderControl=YES;
}
return cell;
} #pragma mark
#pragma mark TableView Delegate
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone; /*
//只有编辑状态时,才有删除功能
//由于手指左划显示“Delete”按钮并不处于编辑状态,所以会被屏蔽掉
if(self.tableView.editing)
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
*/
} -(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
//进入编辑状态时单元行不向右缩进
return NO;
}
运行程序,效果如图:
IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)的更多相关文章
- IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的 下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码. 显示下过如下: #pragma mark #pragma mark Tabl ...
- iOS 表视图(UITableVIew)的使用方法(1)表视图的示例
表视图继承自UIScrollView,所以有着大多UIScrollView的操作特性,诸如手指控制内容的滚动,内容视图到顶端或者低端时的自动反弹等.配合UINavigationController的导 ...
- IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)
在表视图的删除操作中,每次只能够对其中一个单元进行删除,如果想要同时删除多条记录,不得不挨个地进行标准的删除操作 所以如果能够实现多选的机制,无论是删除还是其他功能的嫁接,都会变得更加方便 当UITa ...
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
默认的,如果表视图支持编辑,那用户可以通过两种方式来删除某些行,其一为单击左侧的红色按钮后行右侧显示“Delete”按钮,其二为在单元行上的手指向左滑动,“Delete”按钮也会出现供用户单击.无论哪 ...
- IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示
当数据量特别大时,简单地以role进行分段,对实际查找的效率提升并不大.就像上一节开头所说,开发者可以根据球员名字的首字母进行分段,且分成26段.由于段数较多,可以使用UITableView的索引机制 ...
- IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示
我们可以采用名字分段法,名字分段会在之后的小节中显示,这是转而使用球员的角色分段发,以最直接的入手点讲解好UITableView的分段使用方法.本节示例时基于上节的SimpleTableViewCon ...
- IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元
UITableViewCell的自定义往往需要自建一个UITableViewCell的子类后进行作业.开发者可以选择通过xib或者直接在UITableViewCell的布局中进行UITableView ...
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
随机推荐
- HDU ACM 1046 Gridland 找规律
分析:给出一个矩阵.问最短从一个点经过全部点以此回到起点的长度是多少.绘图非常好理解.先画3*4.3*3.4*4的点阵图案.试着在上面用最短路走一走,能够发现当矩形点阵的长宽都是奇数时,最短路中必然有 ...
- Radio Link Failure and Recovery
四种会发生Radio Link Failure的场景 - DL Physical Layer Failure (PDCCH BLER > 10%) - Random Access Probl ...
- [置顶] PHP如何扩展和如何在linux底层对php扩展?
虽然大部分php工程师都不需要知道php的C代码核心是如何运作的,有些人可能知道有个dl()函数.或者使用过一些第三方的类库,这些正是本文的重点之一. 希望对那些想把php带向更 ...
- web - float , 浮动
浮动 : 使元素脱离文档流,按照指定的方向发生移动,遇到父级边界或者相邻的浮动元素则停下来: 元素被设置浮动属性后,呈现的特征有: 1.多个块可以在一行显示 2.内联元素支持狂傲 3.默认宽度由内容撑 ...
- css position 属性 (absolute 和fixed 区别)
在css3中,position的属性值有:inherit, static, relative ,absolute, fixed. inherit 是继承父元素的position属性值,IE不支持. s ...
- insert into table1 (column1,column2) select column1,column2 from table2 where 条件
sql 语句: insert into table1 (column1,column2) select column1,column2 from table2 where 条件 含义: 将table2 ...
- Windows Server 中开启 SQL Server 2008 的1433端口
在Windows Server2008 服务器上部署了Microsofit SQL Server2008 R2 ,想让远程机器能够访问,于是开放1433端口,进行了如下设置: 1.打开“本地安全策略” ...
- [转载]Heritrix 提高效率的若干方法
摘自http://blog.sina.com.cn/s/blog_6cc084c90100nf39.html --------------------------------------------- ...
- 数字运算、ASCII
num20 = dollar/20;num10 = (dollar - 20*num20)/10;num5 =(dollar-20*num20-10*num10)/5;//可以写为num5 = (do ...
- github--新手使用错误分析
先上去github 或者任意托管的网站.注册账号,新建仓库, 在本地运行Xcode 新建工程,新建工程的时候 勾上本地 的仓库,然后 在本地的项目根目录执行下边的命令: git remote add ...