2012-03-25 18 views
0

セルが選択されているときに通常の(選択されていないcell.backgroundView)イメージを非表示にして、選択されていないときに表示する必要があります。半透明の背景を選択したUITableViewCell

通常のビュー(cell.backgroundView)が常に存在し、セルが選択されると、選択されたイメージ(cell.selectedBackgroundView)がビューにアニメーション化され、通常のビューの上に配置されます。

問題は、選択したセルが半透明で、通常のセルが常にその下に表示される場合です。私が作成した は、私は私のビューコントローラにロードし、私の(カスタム)のUITableViewCellのための2つのビューで:

-(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を設定してみてください;'セルが選択されます。 –

+0

私に追加: - (ボイド)のtableView:(のUITableView *)のtableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[のtableView cellForRowAtIndexPath:indexPath] .backgroundView.alpha =は0.0f。 これは作業をしましたが、私は別のセルを選択してもアルファ= 0でセルを保持します。 – dandan

+0

選択したセル(メンバー変数でのストア参照など)を何とか追跡し、別のセルを選択するとアルファを1.0fに戻す必要があります。 –

答えて

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