【问题标题】:Objective c subview in UITableViewCell background color on select选择时 UITableViewCell 背景颜色中的目标 c 子视图
【发布时间】:2012-12-11 18:37:53
【问题描述】:

大家好,我对 ios 还很陌生,所以还有很多东西要学习,我将一个具有动态背景颜色的子视图从 web 服务添加到表格单元格,但是在选择子视图时,背景颜色正在更改为表格单元格的选定状态颜色,我知道为什么要这样做,因为它会更改视图中的所有子视图背景颜色,我似乎无法弄清楚如何阻止它更改子视图背景颜色,我希望它保持选择的动态颜色?

array = [colors objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UIView *colorBox = [[UIView alloc] initWithFrame:CGRectMake(10,10,20,20)];
        [colorBox setTag:1];            
        [cell.contentView addSubview:colorBox];
    }

 UIView *box = [cell viewWithTag:1];
 box.backgroundColor = [self colorForBox:array.color];

 return cell;

然后获取颜色

- (UIColor *)colorForTransport:(NSString*)Line {

if([Line isEqualToString:@"Brown"])
    return [UIColor colorWithRed:0.682 green:0.38 blue:0.094 alpha:1];

else if([Line isEqualToString:@"Red"])
    return [UIColor colorWithRed:0.894 green:0.122 blue:0.122 alpha:1];

else
    return DefaultBackgroundColor;
}

任何帮助将不胜感激谢谢!

【问题讨论】:

  • 我不明白这个问题。你想阻止什么?

标签: objective-c uitableview addsubview


【解决方案1】:

您可以尝试更改 tablecellview 的 selectedBackgroundView 子视图以获得您正在寻找的颜色变化。

有关该属性的更多信息,请参阅http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/selectedBackgroundView

【讨论】:

    【解决方案2】:

    如果您使用的是普通样式表,则需要分配初始化具有所需背景颜色的新 UIView,然后将其分配给 selectedBackgroundView。

    或者类似的,

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    

    【讨论】:

      【解决方案3】:

      您可以创建继承自 UIView 的新子类。 然后重写 setBackgroundColor 什么都不做。 并添加您自己的方法来设置背景颜色。这样系统将无法覆盖您的背景颜色,您可以使用新方法设置任何颜色:)

      -(void) setBackgroundColor:(UIColor *)backgroundColor
      {
          //do nothing
      }
      
      //System do not know this method :)
      -(void) mySetBackgroundColor:(UIColor *)backgroundColor
      {
          [super setBackgroundColor:backgroundColor];
      }
      

      【讨论】:

        【解决方案4】:

        UITableViewCell 出于某种原因在选择时更改所有子视图的背景颜色。

        这可能会有所帮助:

        DVColorLockView

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-04-04
          • 2010-11-02
          • 1970-01-01
          • 1970-01-01
          • 2015-06-11
          • 2015-01-09
          • 2015-07-06
          • 2012-06-05
          相关资源
          最近更新 更多