如何限制UITableView行重排序到一个部分 [英] How to limit UITableView row reordering to a section

查看:115
本文介绍了如何限制UITableView行重排序到一个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我撞到我的头,这个,谷歌没有什么。

I was hitting my head over this one, and google was turning up nothing. I eventually worked it out and thought I'd write it up here for the sake of the next person.

你有一个 UITableView 包含多个节。每个部分是同质的,但表总体是异构的。因此,您可能希望允许重新排序某个部分中的行,但不允许跨个部分。也许你只想要一个部分是可重新排序的(这是我的情况)。
如果你正在查找,在 UITableViewDataSourceDelegate ,你不会找到一个通知,当它将要让你移动一行之间的部分。你得到一个,当它开始移动一行(这是罚款),一个,当它已经移动它,你有机会与你的内部的东西同步。没有帮助。

You have a UITableView with multiple sections. Each section is homogeneous, but the table overall is heterogeneous. So you might want to allow re-ordering of rows within a section, but not across sections. Maybe you only even want want one section to be reorderable at all (that was my case). If you're looking, as I was, at the UITableViewDataSourceDelegate you won't find a notification for when it is about to let you move a row between sections. You get one when it starts moving a row (which is fine) and one when it's already moved it and you get a chance to sync with your internal stuff. Not helpful.

那么,如何才能阻止部分之间的重新排序?

So how can you prevent re-orders between sections?

推荐答案

此实施将防止重新启动,如Phil的回答那样在原始部分之外排序,但它也会将记录捕捉到该部分的第一行或最后一行,取决于拖动的位置,而不是开始的位置。

This implementation will prevent re-ordering outside of the original section like Phil's answer, but it will also snap the record to the first or last row of the section, depending on where the drag went, instead of where it started.

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
  if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
    NSInteger row = 0;
    if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
      row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
    }
    return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];     
  }

  return proposedDestinationIndexPath;
}

这篇关于如何限制UITableView行重排序到一个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆