【问题标题】:View in UiTableViewCell turns transparent at selectionUiTableViewCell 中的视图在选择时变为透明
【发布时间】:2013-07-23 15:54:35
【问题描述】:

我有一个自定义的UITableViewCell 和一个红色的UIView,当我选择UITableViewCell 时它变成透明的。

我不希望这种情况发生。有没有办法在选择时保持UIView 的颜色?

仅关于该视图的配置。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellIdentifier = @"Cell Identifier";
    DetailsCell *cell = (DetailsCell *) [tableView dequeueReusableCellWithIdentifier: kCellIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DetailsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.myView.layer.cornerRadius = 5;
    cell.myView.layer.masksToBounds = YES;
    return cell;
}

【问题讨论】:

  • 你可以添加你的代码吗?
  • 代码添加到问题

标签: ios uitableview custom-controls selection


【解决方案1】:

为您的视图添加标签。

yourview.tag = 3;

然后在您的 tableView 的 didSelectRowAtIndexPath 方法中添加此代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //get the cell which is selected
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    //set tempView color of selected cell bcoz when cell selected all view color is gone
    UIView *tempView=[selectedCell viewWithTag:3];
//set your color whatever you want
    tempView.backgroundColor = [UIColor colorWithRed:(112/255.0) green:(136/255.0) blue:(213/255.0) alpha:1];
}

更新

您也可以通过将单元格选择颜色选择为 NONE 来执行此操作。将此代码放入cellForRowAtIndexPath

cell.selectionStyle = UITableViewCellSelectionStyleNone;

【讨论】:

  • 差不多就是这样。这样做的问题是,当我选择 UIView 时,它仍然是透明的,只有当我取消选择时,它才会变成选择的颜色。
【解决方案2】:

您还可以使用 UITableViewCell 的 setHighlighted 方法撤消默认行为所做的任何更改。

【讨论】:

  • 解决方案 - 在单元格中:覆盖 func setHighlighted(_ highlight: Bool, animated: Bool) { super.setHighlighted(highlighted, animated: animated) imageView.backgroundColor = = UIColor.white }
【解决方案3】:

您可以使用所需的颜色设置自定义背景视图。您执行此操作的位置会因您配置单元的方式而异。

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = // Your color (cell.backgroundColor perhaps);
cell.selectedBackgroundView = bgColorView;

【讨论】:

  • 我不想更改 UITableViewCell 选择的颜色。这是我在 UITableViewCell 中的视图。
【解决方案4】:

如果您确实想显示选择(使用蓝色)但保留视图的颜色为您提供的颜色,您可以将 selectionStyle 设置为 nil,就像您在答案中所做的那样(或在 IB 中设置),然后在代码中执行此操作:

在 RDCell.m 中:

@implementation RDCell

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundView = [[UIView alloc] init];
        self.backgroundView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

在表格视图控制器中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.label1.text = self.theData[indexPath.row];
    if ([self.selectedPath isEqual:indexPath]) {
        cell.backgroundView.backgroundColor = [UIColor blueColor];
    }else{
        cell.backgroundView.backgroundColor = [UIColor clearColor];
    }
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectedPath = indexPath;
    [self.tableView reloadData];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多