2012-03-25 50 views
0

我需要在選中單元格時隱藏單元格的正常(未選中的 - cell.backgroundView)圖像,並在未選中時顯示它。具有半透明選定背景的UITableViewCell

tableview的工作方式是普通視圖(cell.backgroundView)始終存在,當選中單元格時,它將所選圖像(cell.selectedBackgroundView)動畫化到視圖中,並放置在普通視圖的頂部。

問題是,當選定的單元格是半透明的,正常的單元格總是在它下面可見。 我創建,在2次爲我(自定義)的UITableViewCell我在我的視圖控制器加載:

-(void)tableView:(UITableView *)tableView 
willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XYCell"]]; 
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XYCellSelected"]]; 
} 

我清除顏色在需要的地方,但我不能讓它的工作,因爲我想要的。 由於我選擇的圖像(cell.selectedBackgroundView)是半透明的,cell.backgroundView仍然可以在它下面看到。 我該如何讓它消失?

+0

你可以嘗試設置'cell.backgroundView.alpha = 0.0F;'當細胞被選中。 – 2012-03-25 11:20:20

+0

我添加到我的: - (無效)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath { [的tableView的cellForRowAtIndexPath:indexPath] .backgroundView.alpha = 0.0F; 這樣做的工作,但即使我選擇另一個單元格時,我仍保持單元格爲alpha = 0。 – dandan 2012-03-25 11:50:16

+0

您需要以某種方式跟蹤選定的單元格(例如,成員變量中的存儲引用),並在選擇另一個時將alpha設置回1.0f。 – 2012-03-25 11:57:05

回答

1

一般來說,如果你想要一個自定義的單元格,你應該實現你自己的uitableviewcell。

在你的情況來看看

- (void)setSelected:(BOOL)selected animated:(BOOL)animated; 

代碼示例,以幫助您:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 
    //your own backgroundview when selected 
    self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectedBck.png"]]; 
    if (selected){ 
     // edit the cell's view when it's selected 
     self.backgroundView = nil; 
    } 
    else { 
     // edit the cell's view when it isn't selected 
    } 
} 
1
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.highlightIndexPath = indexPath; // for iOS6 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.backgroundView.hidden = YES; 
} 

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (indexPath.row == NSNotFound) { 
     // for iOS6 
     cell = [tableView cellForRowAtIndexPath:self.highlightIndexPath]; 
    } 
    cell.backgroundView.hidden = NO; 
}