2013-03-05 92 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]; 
    } 
} 
相关问题