Prevent UITableviewcell from becoming transparent

2019-04-04 02:15发布

问题:

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?

回答1:

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



回答2:

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];
}


回答3:

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.


回答4:

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


回答5:

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


回答6:

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.