简体   繁体   中英

Setting alpha of IUTableViewCell subclass subview

I have a subclass of a UITableViewCell. I am trying to adjust the alpha of a whiteLine subview, but the alpha only takes effect once scrolling to an off screen cell. The initial batch of whiteLine subviews display with an alpha of 1.0.

Here's how I set up the table cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CartCell";

    BaseCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[BaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    [cell rowIsOdd:(indexPath.row%2 ? NO : YES)];

    return cell;
}

And here's how I'm setting up whiteLine and altering its alpha within the table cell subclass:

- (void)drawRect:(CGRect)rect
{
    self.whiteLine = [[UIView alloc] initWithFrame:CGRectMake(0.0, self.frame.size.height-1.0, self.frame.size.width, 1.0)];
    self.whiteLine.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.whiteLine];
}

- (void)rowIsOdd:(BOOL)isOdd
{
    self.whiteLine.alpha = (isOdd ? 0.7 : 0.3);
}

Could the problem be that I'm using properties? I never know when not to use properties. This definitely is not a property that is accessible outside of this class.

I figured it out. I needed to setup the view in awakeFromNib instead of drawRect .

You probably want to move your whiteLine subview initialization to initWithStyle:reusedIdentifier: Currently you set up the alpha before it gets instantiated. Also, you're creating a new view every time drawRect: is called, which definitely is a no-no as well.

I'm not currently at the compiler, but something like this should solve your issue:

Note that I also added the autorelease call to your whiteLine subview (I assume it is a retained property). You may want to consider using ARC if you're not comfortable with Cocoa Memory Management. Otherwise I suggest re-reading Apple's Memory Management guide and possibly excellent Google Objective-C Code Style Guide

In BaseCell.m:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)identifier {
    self = [super initWithStyle:style reuseIdentifier:identifier];
    if (self) {
        self.whiteLine = [[[UIView alloc] initWithFrame:CGRectMake(0.0,  self.frame.size.height-1.0, self.frame.size.width, 1.0)] autorelease];
        self.whiteLine.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.whiteLine];
    }
    return self;
}

- (void)dealloc {
    self.whiteLine = nil;
    [super dealloc];
}

- (void)rowIsOdd:(BOOL)isOdd
{
    self.whiteLine.alpha = (isOdd ? 0.7 : 0.3);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM