2012-02-04 61 views
0

我有一个UITableViewCell的子类。我正在尝试调整whiteLine子视图的阿尔法,但该阿尔法仅在滚动到屏幕外单元格后才生效。最初一批whiteLine子视图显示alpha为1.0。设置IUTableViewCell子类子视图的alpha

下面是我如何设置表格单元格:

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

这里就是我如何建立whiteLine和表格单元格子内改变其阿尔法:

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

可能的问题是我正在使用属性?我永远不知道何时不是使用属性。这绝对不是可以在此课程之外访问的属性。

回答

0

我想通了。我需要在awakeFromNib而不是drawRect中设置视图。

0

您可能想要将whiteLine子视图初始化移动到initWithStyle:reusedIdentifier:目前,在设置alpha之前,它将被实例化。另外,每次调用drawRect时,都会创建一个新视图,这绝对是一个不错的选择。

我不是目前的编译器,但这样的事情应该解决您的问题:

请注意,我还添加了自动释放呼叫您的WHITELINE子视图(我假设它是一个保留的属性)。如果您对可可内存管理不满意,您可能需要考虑使用ARC。否则,我建议重新阅读Apple's Memory Management引导和可能优良Google Objective-C Code Style Guide

在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); 
}