2013-03-05 93 views
0

我有一個應用程序,通過使用cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_gray_with_line.png"]];來更改所選單元格的顏色。但是,當我選擇它是不會改變我的背景回到以前的視圖didselect,推如何在推送後更改所選單元格的背景顏色?

if (indexPath != nil) { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES];  
} 

我需要相同的背景爲正常彈出時返回到該視圖控制器,無需重新加載後後這樣做表視圖。有誰能夠幫助我?

回答

1

您應該爲您的UITableViewCell設置兩個屬性。

  1. backgroundView
  2. selectedBackgroundView

此外,你應該設置cell.selectionStyleUITableViewCellSelectionStyleNone

+0

我這樣做BU的backgroundColor一些reasons.that是這裏的問題 – hacker 2013-03-05 07:07:31

0

這應該做的,

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow] ].backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"normal.png"]]; 
} 
0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    //First you get the cell you want to change 
    UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath]; 

    //Then you change the properties (label, text, color etc..) in your case, the background color 
    theCell.contentView.backgroundColor=[UIColor redColor]; 

    //Deselect the cell so you can see the color change 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
1

UITableViewControllers會,如果他們的clearsSelectionOnViewWillAppear屬性設置爲YES,取消對viewWillAppear中選定行。如果你不使用表格視圖控制器與設置(默認是YES,我認爲)自己動手:

- (void)viewWillAppear:(BOOL)animated { 

    [super viewWillAppear:animated]; 

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

落實做了(或會)取消委託方法和解決您的色彩:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    // do what you want to the cell 
} 

順便說一下,其他人SO建議設置該單元格的內容視圖(不cell.backgroundColor,但cell.contentView.backgroundColor)的顏色,並在willDisplayCell:forRowAtIndexPath:這樣做。

+0

我做到了,但沒有解決。我不能重新加載tableview。 – hacker 2013-03-05 07:47:39

0

與@dand相同的答案,但修改了一下。

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

    NSIndexPath *indexPath = [self.tableview indexPathForSelectedRow]; 
    [self.tableview deselectRowAtIndexPath: indexPath animated: YES]; 
} 

並在您的自定義單元實現中修改此方法。

- (void) setSelected: (BOOL) selected animated: (BOOL) animated 
{ 
    [super setSelected: selected animated: animated]; 
    // Configure the view for the selected state 

    if (selected) 
    { 
     self.backgroundColor = [UIColor redColor]; 
    } 
    else 
    { 
     self.backgroundColor = [UIColor clearColor]; 
    } 
} 
相關問題