在UITableViewCell ios 7.1.2中更改默认删除按钮的颜色

时间:2014-07-08 09:25:09

标签: ios uitableview ios7.1

我开发了一款应用。并更改了删除按钮的默认红色。它工作得很清楚。但是在我升级我的iPad iOS 7.1.2之后,它的功能并不多。 这是我用过的代码

 for (UIView *subview in self.subviews) {
        for(UIView *subview2 in subview.subviews){
            if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"] ||) {
                ((UIView*)[subview2.subviews firstObject]).backgroundColor=[UIColor blueColor];
            }
        }
    }

3 个答案:

答案 0 :(得分:7)

问题是您尝试在错误的时间访问 UITableViewCellDeleteConfirmationButton

似乎Apple更改了实现,以便在触发 willTransitionToState:之后,触发 didTransitionToState:

访问 didTransitionToState:中的按钮为时已晚。拖动它时会有原始颜色,只有在完成滑动后才会改变颜色。

解决方法是在线程的运行循环中对方法进行排队,而不是立即执行它,允许操作系统在此期间添加“删除”按钮视图。

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {
        [self performSelector:@selector(setupDeleteButton) withObject:nil afterDelay:0];
    }
}

- (void)setupDeleteButton
{
    [self recurseToDeleteButtonInViews:[self subviews]];
}

- (void)recurseToDeleteButtonInViews:(NSArray *)subviews
{
    for (UIView *subview in subviews)
    {
    if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]){
            // You just found your button!
            // Do what ever you want with it!
            return;
        }
        if ([[subview subviews] count] > 0){
            [self recurseToDeleteButtonInViews:[subview subviews]];
        }
    }
    return;
}

P.S。此代码不向后兼容iOS 6.0,因为在iOS 7中更改了按钮视图的名称

答案 1 :(得分:0)

此方法适用于自定义单元格。

-(void)willTransitionToState:(UITableViewCellStateMask)state    
{

     [super willTransitionToState:state];
     if ((state & UITableViewCellStateShowingDeleteConfirmationMask) ==UITableViewCellStateShowingDeleteConfirmationMask) 
     {
        for (UIView *subview in self.subviews) 
     {
        if ([NSStringFromClass([subview class])isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
                UIImageView *delbtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 20)];
                [delbtn setImage:[UIImage imageNamed:@"del.png"]];
                [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];

            }
        }
    }
}

答案 2 :(得分:0)

我有同样的问题,这个解决方法做了我想要的。

将此行添加到您的单元格的自定义初始化。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];

    if (self)
    {
        [self.contentView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
    }
}

然后你需要处理观察者。

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"frame"])
    {
        [self fixDeleteButtonColor];
    }
}

现在,我基本上做同样的事情(就像其他回复一样)

- (void)fixDeleteButtonColor
{
    for (UIView *view in self.subviews)
    {
       //is not going to be in our contentView, and we may have many views there..
        if (view == self.contentView)
        {
            continue;
        }
        [self checkSubViews:view];
    }
}

- (void)checkSubViews:(UIView *)view
{
    // may be better to just compare the strings...
    if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"])
    {
        [view setBackgroundColor:[UIColor blackColor]];
        UIView *subView = view.subviews[0];
        subView.backgroundColor = [UIColor blackColor];
    }
    else if (view.subviews.count)
    {
        for (UIView *subView in view.subviews)
        {
            [self checkSubViews:subView];
        }
    }
}

并且,不要忘记删除dealloc上的观察者

-(void)dealloc
{
    [self.contentView removeObserver:self forKeyPath:@"frame"];
}
相关问题