如何在左侧对 UITableViewCell 进行重新排序控制? [英] How do I make reorder control of UITableViewCell in left side?

查看:29
本文介绍了如何在左侧对 UITableViewCell 进行重新排序控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个新闻阅读器应用程序,我想让它让用户可以选择显示/隐藏新闻类别(例如热门新闻、商业、技术、体育等)并像 BBC 新闻应用程序一样重新排序它们安卓.

I am doing a news reader app and I want to make it so that the user can choose show/hide news categories (such as top news, business, technology, sports, etc) and reorder them like the BBC news app in Android.

见下图:

我的问题是:

  • 如何在单元格左侧进行重新排序控制?(在编辑模式下,它的默认位置在单元格的右侧)
  • 我有一个复选框自定义控件.我如何将它放在单元格的右侧?

推荐答案

回答【如果你没有使用任何配件(细节披露,&c)】

(0) 我假设你已经设置好了你的桌子,&c.

Answer [If you're not using any accessories (detail disclosure, &c)]

(0) I assume you have your tables set up, &c.

(1) 此解决方案仅适用于您的表格单元格始终可拖动的情况.将此在您的 viewDidLoad 中添加到您的 Table View Controller .m 文件中.

(1) This solution only works if your tables cells are always draggable. Add this in your viewDidLoad to your Table View Controller .m file.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setEditing:YES];
}

(2) 为了使您可以重新排列单元格,请将 cell.showsReorderControl = YES; 添加到您的 tableView:cellForRowAtIndexPath:.

(2) To make it so you can reorder your cells, add cell.showsReorderControl = YES; to your tableView:cellForRowAtIndexPath:.

(3) 确保你有 tableView:canMoveRowAtIndexPath: 和 tableView:moveRowAtIndexPath:toIndexPath: 方法.

(3) Ensure that you have the tableView:canMoveRowAtIndexPath: and tableView:moveRowAtIndexPath:toIndexPath: methods.

- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES; 
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}

(4) 因为你想要左边的重新排序控件,所以我们必须去掉通常在那里的 Delete 圆圈,而 tableView:editingStyleForRowAtIndexPath: 方法就是这样做的.

(4) Because you want the reorder control on the left, we have to take away the Delete circle usually there and the tableView:editingStyleForRowAtIndexPath: method does that.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

(5) 最后一步,神奇的地方 - 添加 tableView:willDisplayCell:forRowAtIndexPath: 方法,搜索单元格的子视图,将其缩小到私有 UITableViewCellReorderControl,最后覆盖它.

(5) Last step, where the magic happens - add the tableView:willDisplayCell:forRowAtIndexPath: method, search for the cell's subviews, narrow it down to the private UITableViewCellReorderControl, and finally override it.

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    for(UIView* view in cell.subviews)
    {
        if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
        {
            // Creates a new subview the size of the entire cell
            UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
            // Adds the reorder control view to our new subview
            [movedReorderControl addSubview:view];
            // Adds our new subview to the cell
            [cell addSubview:movedReorderControl];
            // CGStuff to move it to the left
            CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width - view.frame.size.width, movedReorderControl.frame.size.height - view.frame.size.height);
            CGAffineTransform transform = CGAffineTransformIdentity;
            transform = CGAffineTransformTranslate(transform, -moveLeft.width, -moveLeft.height);
            // Performs the transform
            [movedReorderControl setTransform:transform];
        }
    }
}

我花了将近十个小时试图找出配件时的解决方案,但我无法做到.我首先需要更多的经验和知识.简单地对披露按钮上的重新排序控件执行相同的操作是行不通的.所以我希望这暂时有效;如果我将来弄明白了,我一定会更新这个.

I spent nearly ten hours trying to figure out a solution for when accessories and I couldn't do it. I need more experience and knowledge first. Simply doing the same thing to the reorder control on the disclosure button didn't work. So I hope this would works for now; and if I figure it out in the future I'll be sure to update this.

Hoang Van Ha,这是我的第一个回答,一个月前我才开始学习 Objective-C,所以我还是个菜鸟.

Hi Hoang Van Ha, this is my first answer ever, and I only started learning Objective-C a month ago, so I'm still quite a noob.

我一直在尝试用我的应用程序做同样的事情,并且已经搜索了几个小时如何去做.在这方面,Apple 的文档并没有多大帮助.当人们只是简单地链接到文档时,我发现这很令人沮丧.我想骂他们.希望我的回答对你有帮助.

I've been trying to do the same thing with my app and have been searching for hours how to do it. Apple's documentation isn't very much help when it comes to this. I found it quite frustrating when people would simply link to the documentation. I wanted to yell at them. I hope my answer helped you.

b2Cloud 赞不绝口,因为我使用了他们来自 这篇文章 并针对我的回答进行了改编.

Lots of kudos to b2Cloud because I used some of their code from this article and adapted it for my answers.

这篇关于如何在左侧对 UITableViewCell 进行重新排序控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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