2014-02-20 34 views
3

正在將iOS6應用轉換爲iOS7,他們正在使用它來設置分組的tableview樣式。在iOS7中,cell.selectedBackgroundView未顯示

cell.backgroundView = aView; 
    cell.selectedBackgroundView = bView; 

當應用程序加載時,它正確地加載backgroundView,但是當我點擊單元格中的selectedBackgroundView不再iOS7工作。點擊選擇單元格,以便工作,但selectBackgroundView只是不顯示。

有什麼建議嗎?我唯一能想到的就是不使用selectedBackgroundView,並且每次選擇和取消選中時,都會在單元格中添加和刪除子視圖。

/// UPDATE ///

把這個在我的cellForRowAtIndex和仍然沒有工作。

UIView *bgColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez 
    bgColorView.layer.masksToBounds = YES; 
    cell.selectedBackgroundView = bgColorView; 

////更新2 ///////////

如果我把這個在我的cellforRowAtIndex

cell.selectedBackgroundView = nil; 
    cell.backgroundView = nil; 
    UIView *bgColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez 
    bgColorView.layer.masksToBounds = YES; 
    cell.selectedBackgroundView = bgColorView; 

    UIView *bgSColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    bgSColorView.backgroundColor = [UIColor colorWithRed:(106.0/255.0) green:(201.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez 
    bgSColorView.layer.masksToBounds = YES; 
    cell.backgroundView = bgSColorView; 

兩個展會cell.background爲bgSColorView ,但是當我單擊單元格時沒有任何反應(又名selectedBackgroundView不顯示?爲什麼選擇的背景視圖不被提升和顯示?

/// UPDATE 3 //// 我把t他在我的setSelectin:animation:爲我的單元格類(刪除了上面的東西)。

UIView *bgColorView = [[UIView alloc] init]; 
bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez 
bgColorView.layer.masksToBounds = YES; 
self.selectedBackgroundView = bgColorView; 

UIView *bgSColorView = [[UIView alloc] init]; 
bgSColorView.backgroundColor = [UIColor colorWithRed:(106.0/255.0) green:(201.0/255.0) blue:(25.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez 
bgSColorView.layer.masksToBounds = YES; 
self.backgroundView = bgSColorView; 



[super setSelected:selected animated:animated]; 

現在看來正確,因爲我想要它。但第二個我點擊任何單元格BOOOM的背景和selectionBackgroundView即時刪除,消失。

+0

在7蘋果確實與細胞背景是什麼。他們在背景的「上方」滑動了一層不透明的白色圖層,並且它只會在cell * cellForCowForTowAtIndexPath(在XIB中不足)執行setBackgroundColor時纔會消失。不確定這可能會如何影響背景圖片,但您可能需要設置背景顏色以清除。 –

+0

你是否繼承了'UITableViewCell'? – sooper

+0

是的,它被分類。但我只是改變了它的子類UITableViewCell,仍然是相同的結果。 – jdog

回答

13

我最近有過這個問題。

發現我的問題是在我的.xib我把selectionStyle設置爲UITableViewCellSelectionStyleNone。

一旦我設置self.selectionStyle = UITableViewCellSelectionStyleDefault;我selectedBackgroundView開始正確顯示。

所以我做到了。

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 

    self.selectionStyle = UITableViewCellSelectionStyleDefault; 

    UIView *selectedBackgroundView = [[UIView alloc] init]; 
    selectedBackgroundView.backgroundColor = [UIColor redColor]; 
    self.selectedBackgroundView = selectedBackgroundView; 
} 
2

每當我不能讓它在的tableView工作:的cellForRowAtIndexPath:我剛子類的UITableViewCell並重寫的setSelected:像這樣的動畫方法:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
    UIImage *image = [UIImage imageNamed:@"selected_cell.png"]; 
    self.selectedBackgroundView = [[UIImageView alloc]initWithImage:image]; 
} 

我只是在不同的項目嘗試這樣做,它沒不工作..哈哈。這裏是我現在使用的:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
     { 
      [super setSelected:selected animated:animated]; 

      if (selected) { 
       contentView.backgroundColor = SharedColors.Blue.ToUIColor(); 
      } else { 
       contentView.backgroundColor = SharedColors.Black.ToUIColor(); 
      } 
     } 
+0

但是,所有這些都是在建立tableview時設置的。我會這樣做,而不是每次選擇單元格。如果我將它設置在cellForRowAt中,我不應該在每個setSelection上設置selectedBackgroundView ...正確嗎? – jdog

+0

你是對的。我不知道爲什麼你的cell.selectedBackgroundView = bView不工作,所以我只是想讓你知道一個解決方法,如果你需要它。另一種可能性是使用這種委託方法: - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 它在繪製單元格之前被調用,是另一個好的解決方法。 – eckyzero

+0

試過了,它不起作用。 – jdog

0
override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
    var view = UIView(frame: self.bounds) 
    view.backgroundColor = UIColor.redColor() 
    self.selectedBackgroundView = view 


}