UITableView 中彻底禁止移动某行

<p>
在UITableView中,我们可以使用- (BOOL) tableView: (UITableView *) tableView canMoveRowAtIndexPath: (NSIndexPath *) indexPath方法来禁止移动某一行。下面的例子是禁止移动最后一行。但是,虽然不能移动最后一行,却可以将其他行移动至最后一行下方。
</p>
<code>

  • (BOOL)tableView:(UITableView *)tableView
    canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSArray *items = [[BNRItemStore sharedStore] allItems];

    if (indexPath.row + 1 == [items count]) {
    return NO;
    }
    return YES;
    }
    </code>

<p>我们可以使用- (NSIndexPath *) tableView: (UITableView *) tableView
targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath *) source
toProposedIndexPath: (NSIndexPath *) destination 方法来彻底禁止移动最后一行,使最后一行始终位于试图的底部。例子如下:</p>

<code>
//prevent rows from being dragged to the last position:
-(NSIndexPath *) tableView: (UITableView *) tableView
targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath *) source
toProposedIndexPath: (NSIndexPath *) destination
{
NSArray *items = [[BNRItemStore sharedStore] allItems];
if (destination.row < [items count] - 1) {
return destination;
}
NSIndexPath *indexPath = nil;
// If your table can have <= 2 items, you might want to robusticize the index math.
if (destination.row == 0) {
indexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
} else {
indexPath = [NSIndexPath indexPathForRow: items.count - 2
inSection: 0];
}
return indexPath;
}
</code>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 7,900评论 2 10
  • 一.进行重载数据的API 注意点:在操作这些方法的时候,记得要修改和保存数据来源,根据测试,该方法调用之后,会调用...
    讨厌下雨的鱼阅读 6,323评论 0 1
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,951评论 3 38
  • 自定义单元格 表格无论有多少中自定义单元格样式 每一种自定义单元格都有复用的能力所以每一个单元格都要带有一个静态局...
    DVWang阅读 1,878评论 0 0
  • 静静放置在那的芭蕾舞鞋,在柔光中仿佛等待着穿他的人出现,跟着它一起去谱写属于他们的故事
    亚洲初恋阅读 1,326评论 0 0