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

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

问题描述

我正在做一个新闻阅读器应用程序,我希望用户可以选择显示/隐藏新闻类别(如热门新闻,商业,技术,体育等),并将它们重新排序为像Android中的BBC新闻应用程序。

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

见下图:

See picture below:

我的问题是:


  • 如何在单元格的左侧进行重新排序控制?
    (默认位置在编辑模式的单元格右侧)

  • 我有复选框自定义控件,如何将它放在单元格的右侧?

提前感谢!!!

推荐答案

答案[如果您没有使用任何配件(详细信息披露,& 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:methods。

(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)因为你想要左边的重新排序控件,我们必须带走那里的删除圈和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天全站免登陆