如何在编辑时更改自定义UITableViewCell上的缩进量? [英] How can I change the amount of indentation on my custom UITableViewCell while editing?

查看:107
本文介绍了如何在编辑时更改自定义UITableViewCell上的缩进量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个自定义的UITableViewCell,并做了正确的(添加一切到 contentView ,并覆盖 layoutSubviews 我的子视图相对于 contentView.bounds )。

I have made a custom UITableViewCell, and done it properly (adding everything to contentView, and overriding layoutSubviews so my subviews are relative to contentView.bounds).

当用户按下编辑按钮时,表缩进允许空间为红色删除标志。这很好,但是默认的缩进量太多,并且破坏了我的自定义单元格的外观。如何减少缩进量? setIndentationLevel tableView:IndentationLevelForRowAtIndexPath 似乎没有做任何事情。

When the user presses the Edit button, the table indents to allow room for the red delete sign. This is fine, but the default amount of indentation is too much, and ruins the look of my custom cell. How can I reduce the amount of indentation? setIndentationLevel and tableView:IndentationLevelForRowAtIndexPath don't seem to do anything.

(有人提出类似问题这里

(Someone asked a similar question here, but it was never resolved).

你必须重写 layoutSubviews

解决方案

c $ c>并执行类似下面的操作。并且不要忘记将缩进级别设置为大于0的对象:)对于自定义单元格,默认情况下不应用缩进级别。

You've got to override layoutSubviews and do something like the following. And don't forget to set the indentation level to something greater than 0 :) For custom cells the indentation level is not applied by default.

为了避免单次滑动删除手势的缩进,你必须做更多的工作。有一个状态反映单元格的编辑状态。它不是public,但可以通过 - (void)willTransitionToState:(UITableViewCellStateMask)aState 访问,因此将其存储属性为layoutViews工作。

To avoid indentation for the single swipe to delete gesture you'll have to do a but more work. There is a state which reflects the editing state of the cell.It is not public but can be accessed with - (void)willTransitionToState:(UITableViewCellStateMask)aState, so storing it in a property does the work for layoutViews.

Apple的 willTransitionToState 文档


当用户刷新单元
以将其删除时,单元转换到

UITableViewCellStateShowingDeleteConfirmationMask
常数标识的状态,而
UITableViewCellStateShowingEditControlMask
是未设置。

Note that when the user swipes a cell to delete it, the cell transitions to the state identified by the UITableViewCellStateShowingDeleteConfirmationMask constant but the UITableViewCellStateShowingEditControlMask is not set.

头文件

header file

int state;

...

@property (nonatomic) int state;

...

单元格实施 / p>

cell implementation

@synthesize state;

...

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.contentView.frame = CGRectMake(0,                                          
                                        self.contentView.frame.origin.y,
                                        self.contentView.frame.size.width, 
                                        self.contentView.frame.size.height);

    if (self.editing
        && ((state & UITableViewCellStateShowingEditControlMask)
        && !(state & UITableViewCellStateShowingDeleteConfirmationMask)) || 
            ((state & UITableViewCellStateShowingEditControlMask)
         && (state & UITableViewCellStateShowingDeleteConfirmationMask))) 
    {
        float indentPoints = self.indentationLevel * self.indentationWidth;

        self.contentView.frame = CGRectMake(indentPoints,
                                            self.contentView.frame.origin.y,
                                            self.contentView.frame.size.width - indentPoints, 
                                            self.contentView.frame.size.height);    
    }
}

- (void)willTransitionToState:(UITableViewCellStateMask)aState
{
    [super willTransitionToState:aState];
    self.state = aState;
}

这篇关于如何在编辑时更改自定义UITableViewCell上的缩进量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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