2012-02-04 62 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); 
}