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

查看:28
本文介绍了如何将 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?

我会将我所做的作为单独的答案发布,让其他人发布更好的答案!

I'll post what I did as a separate answer, leaving it open for someone else to post an even better answer!

推荐答案

此实现将防止像 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天全站免登陆