2014-10-11 63 views
1

我想更改所選單元格背景顏色並在該單元格上保留文本。然後,我確實喜歡這樣,它可以改變單元格的顏色,但單元格上的文字將會消失。如何更改所選單元格背景顏色而不丟失文本

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"MessageCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    cell.textLabel.text = [self.receivedData objectAtIndex:indexPath.row]; 

    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *CellIdentifier = @"MessageCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    UIView *bgColorView = [[UIView alloc]init]; 
    bgColorView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5]; 

    cell.selectedBackgroundView = bgColorView; 

} 
+0

是什麼顏色的文字? – Chris 2014-10-11 18:58:37

+0

[更改選定單元格的背景顏色?]的可能重複(http://stackoverflow.com/questions/2418189/iphone-uitableviewcell-changing-background-color-of-selected-cell) – jww 2014-10-11 19:42:20

+0

您不應該出列新的細胞。獲取對使用表視圖方法cellForRowAtIndexPath:選擇的單元格的引用。 – rdelmar 2014-10-11 19:50:59

回答

0

要更換當前視圖(已收到你的短信在其中的一個)用的UIView的一個新實例(其中不帶文字,但新的顏色)。這就是爲什麼你的文字正在消失。你應該改變selectedBackgroundview的背景顏色屬性,像這樣:

cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5]; 

雖然我覺得你甚至可以溝selectedBackgroundView一部分,只是做cell.backgroundColor = ...didSelectRowAtIndexPath

相關問題