除了每个单元行左边的删除和新增图标,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)表视图的编辑功能(拖拉调整排序位置)的更多相关文章

  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)的使用方法(5)表视图的编辑功能(删除)

    默认的,如果表视图支持编辑,那用户可以通过两种方式来删除某些行,其一为单击左侧的红色按钮后行右侧显示“Delete”按钮,其二为在单元行上的手指向左滑动,“Delete”按钮也会出现供用户单击.无论哪 ...

  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. xcode UIImage图片拉伸

    图片拉伸 +(UIImage*)wlisWithImage:(NSString *)name{ //获取图片 UIImage * img=[UIImage imageNamed:name]; //获取 ...

  2. 性能优化工具---top

    作用: 实时显示linux下各个进程的资源占用情况 参数: -d :后面可以接秒数,就是整个程序画面更新的秒数.预设是 5 秒: -p :指定某些个 PID 来进行观察监测而已. -b :以批次的方式 ...

  3. ASPxGridView-为每行添加序号

    添加一个新的非绑定列,使用CustomColumnDisplayText事件来分配序号给该列 <dx:GridViewDataTextColumn Caption="序号" ...

  4. select实现选中跳转

    select选择后直接跳转到其他网站的三种方式     第一种: ************************** <html> <head> <meta http- ...

  5. STL模板_概念

    模板和STL一.模板的背景知识1.针对不同的类型定义不同函数版本.2.借助参数宏摆脱类型的限制,同时也因为失去的类型检查而引 入风险.3.借助于编译预处理器根据函数宏框架,扩展为针对不同类型的 具体函 ...

  6. Hadoop插件安装

    1.首先下载Hadoop对应版本的插件,以Hadoop 1.0版本对应的插件Hadoop-eclipse-plugin1.0.3.jar为例 2.将下载的插件放置到Ecplise安装目录的plugin ...

  7. C++から広がり

    泛型编程(Generic Programming)最初提出时的动机很简单直接:发明一种语言机制,能够帮助实现一个通用的标准容器库.所谓通用的标准容器库,就是要能够做到,比如用一个List类存放所有可能 ...

  8. windows中使用Git工具连接GitHub(配置篇)

    Git在源码管理领域目前占很大的比重了,而且开源的项目很多都转到GitHub上面了.例如:jQuery, reddit, Sparkle, curl, Ruby on Rails, node.js,  ...

  9. cocos2dx mac下搭建android开发环境

    1)下载eclipse 地址:http://www.eclipse.org/downloads/ 2)安装adt 打开eclipse,菜单:help->install new software ...

  10. 多线程编程之二 ---MFC中的多线程开发

    下载源代码 五.MFC对多线程编程的支持 MFC中有两类线程,分别称之为工作者线程和用户界面线程.二者的主要区别在于工作者线程没有消息循环,而用户界面线程有自己的消息队列和消息循环. 工作者线程没有消 ...