简体   繁体   English

推后如何更改所选单元格的背景颜色?

[英]How to change the background color of the selected cell after pushing?

I have an application in which I am changing the color of the selected cell by using the cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_gray_with_line.png"]]; 我有一个应用程序,我通过使用cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_gray_with_line.png"]];改变所选单元格的颜色cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_gray_with_line.png"]]; . But when I am doing this after selecting in the didselect it is not changing my background back to the previous view, after pushing 但是当我在选择didselect后执行此操作时,在推动后不会将我的背景更改回上一个视图

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

I need the same background as normal when popping back to this view controller, without reloading the table view. 弹出回到此视图控制器时,我需要与正常情况相同的背景,而无需重新加载表视图。 Can anybody help me? 有谁能够帮助我?

You should set two properties to your UITableViewCell . 您应该为UITableViewCell设置两个属性。

  1. backgroundView backgroundView
  2. selectedBackgroundView selectedBackgroundView

Also, you should set cell.selectionStyle to UITableViewCellSelectionStyleNone . 此外,您应将cell.selectionStyle设置为UITableViewCellSelectionStyleNone

UITableViewControllers will, if their clearsSelectionOnViewWillAppear property is set to YES, deselect the selected row on viewWillAppear. 如果将其clearsSelectionOnViewWillAppear属性设置为YES,UITableViewControllers将取消选择viewWillAppear上的选定行。 If you're not using a table view controller with that setting (default is YES, I think), do it yourself: 如果您没有使用具有该设置的表视图控制器(我认为默认为YES),请自己动手:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

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

Implement the did (or will) deselect delegate method and fix your color: 实现did(或将)取消选择委托方法并修复颜色:

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

Incidentally, others on SO recommend setting the color on the cell's content view (not cell.backgroundColor , but cell.contentView.backgroundColor ), and doing so in willDisplayCell:forRowAtIndexPath: . 顺便提一下,SO上的其他人建议在单元格的内容视图上设置颜色(不是cell.backgroundColor ,而是cell.contentView.backgroundColor ),并在willDisplayCell:forRowAtIndexPath:这样做。

This should do, 这应该做的,

- (void)viewDidAppear:(BOOL)animated
{
 [super viewDidAppear:animated];
 [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow] ].backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"normal.png"]];
}
- (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];

}

same answer as @dand, but modify a little bit. 与@dand相同的答案,但稍微修改一下。

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

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

and modify this method in your custom cell implementation. 并在自定义单元格实现中修改此方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM