开发者

Prevent UITableviewcell from becoming transparent during reordering

I am trying to keep my uitableviewcells opaque during reorder animation - reason being that I am doing custom drawing of the cell in one view to enhance scroll performance, but doing this 开发者_如何转开发slows down the reorder animation significantly.

Any idea how to achieve this?


For an excellent description of what is going on when the table is in dragging mode, see this answer.

I had a UITableViewStyleGrouped on iOS 7. In order to get the desired effect (non-transparent, white background), I had to follow Aaron's suggestion and bmovement's.

@interface ABCCell : UITableViewCell

@end

@implementation ABCCell

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundView = [UIView new];
        self.backgroundView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setAlpha:(CGFloat)alpha {
    [super setAlpha:1.0];
}

@end

Not doing either causes the view to have a 100% transparent background.

Setting just the background view causes the cell to have a 80% transparent white background.

Setting just the alpha causes the cell to have a black background (0% transparent).

Implementing both, gives you a white background (0% transparent).


For some reason, my UITableViewCell subclass's drawRect wasn't being called when the reorder animation began, but adding this to my subclass worked for me:

- (void)setAlpha:(CGFloat)alpha {
    [super setAlpha:1.0f];
}


This is an old question, but this issue can come up again for cells in table views with style UITableViewStyleGrouped on iOS 7. These cells should have a non-nil background view by default, but staring in iOS 7 the background view is either not set or is not given an opaque background. To prevent the cells from becoming transparent during reorder, add code like this in the cell class's initWithStyle:reuseIdentifier: method (or somewhere else around the initialization of the cells). Here self is the cell itself.

self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
self.backgroundView.backgroundColor = [UIColor whiteColor]; // Or whatever color.


Here's a solution for Swift.

class StopMakingMeTransparentCell: UITableViewCell {
    override var alpha: CGFloat {
        didSet {
            super.alpha = 1
        }
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.backgroundView = UIView()
        self.backgroundView!.backgroundColor = UIColor.whiteColor()
    }
}


An most up to date answer for this problem, in Swift 3:

        let fakeBgColorImageView = UIImageView(image:UIImage(color:UIColor.blue))
        addSubview(fakeBgColorImageView)
        // Autolayout with PureLayout, use whatever you want here
        fakeBgColorImageView.autoPinEdgesToSuperviewEdges()
        sendSubview(toBack: fakeBgColorImageView)

  1. Add this code to the initialisers of a custom cell
  2. Add the fake UIImageView with color as the background of the cell (NOT the content view!, because it will be resized and won't cover the reordering control, neither the (-) delete icon)
  3. Send the view to the back, so it's behind the contentView


I feel dumb for asking this question XD

I just figured out that in my drawrect function, I can override alpha value. so, in the beginning of the draw, I just set self.alpha = 1.0f, and voila my cells are opaque in all modes, including in middle of reordering.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜