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

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

问题描述

我正在做一个新闻阅读器应用程序,我想制作它,以便用户可以选择显示/隐藏新闻类别(例如热门新闻,商业,技术,体育等),并像在BBC新闻应用程序中那样对它们进行重新排序。 Android。



请参见下图:





我的问题是:




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

  • 我有一个复选框自定义控件。如何将其放在单元格的右侧?


解决方案

答案[如果不使用任何配件(详细信息,& c)]


(0)我假设您已经设置好表,& c。


(1)仅当表格单元始终可拖动时,此解决方案才有效。

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

(2)要使其重新排序,请添加 cell.showsReorderControl = YES; 到tableView:cellForRowAtIndexPath:。


(3)确保您具有tableView:canMoveRowAtIndexPath:和tableView:moveRowAtIndexPath:toIndexPath:

 -(BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{
返回是;
}

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

(4)因为您想要左侧的重新排序控件,所以我们必须删除通常在那里的Delete圆圈和tableView:

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

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

 -(void)tableView:(UITableView *) tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
for(cell.subviews中的UIView *视图)
{
if([[[view class ] description] isEqualToString:@ UITableViewCellReorderControl])
{
//创建一个新的子视图,其大小为整个单元格的大小
UIView * movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake( 0,0,CGRectGetMaxX(view.frame),CGRectGetMaxY(view.frame))];
//将重新排序控件视图添加到我们的新子视图
[movedReorderControl addSubview:view];
//将新的子视图添加到单元格
[cell addSubview:movedReorderControl];
// CGStuff将其向左移动
CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width-view.frame.size.width,moveReorderControl.frame.size.height-view.frame。 size.height);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform,-moveLeft.width,-moveLeft.height);
//执行转换
[movedReorderControl setTransform:transform];
}
}
}

我花了近十个小时试图找出一个解决配件无法使用的问题。我首先需要更多的经验和知识。仅对披露按钮上的重新排序控件执行相同的操作是行不通的。因此,我希望现在可以使用;


您好,Hoang Van Ha,这是我有史以来的第一个答案,我


我只是一个月前才开始学习Objective-C,所以我还是一个菜鸟。


我一直在尝试用我的应用程序做同样的事情,并且一直在寻找小时如何去做吧。 Apple的文档在这方面不是很有帮助。当人们仅链接到文档时,我就感到非常沮丧。我想对他们大喊大叫。希望我的回答对您有所帮助。


b2Cloud 表示敬意因为我使用了 this中的一些代码文章,并将其修改为我的答案。



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.

See the picture below:

My question is:

  • How do I make reorder control in left side of cell? (Its default position is in the right side of the cell in edit mode)
  • I have a checkbox custom control. How do I put it in right side of the cell?

解决方案

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

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

(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) To make it so you can reorder your cells, add cell.showsReorderControl = YES; to your tableView:cellForRowAtIndexPath:.

(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) 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) 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.

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.

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.

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天全站免登陆