【问题标题】:highlight a part of the cell when selected选中时突出显示单元格的一部分
【发布时间】:2011-11-22 20:10:32
【问题描述】:

我正在尝试在单元格突出显示时为其添加非标准颜色。为此,我创建了一个具有所需背景颜色的视图,并将其设置为单元格的selectedBackgroundView

一切都很好。

UIView *selectionView = [[UIView alloc] init];
[selectionView setBackgroundColor:[UIColor colorWithRed:(121/255.0) green:(201/255.0) blue:(209/255.0) alpha:1.0]];
[cell setSelectedBackgroundView:selectionView];

我的问题,我可以更改selectedBackgroundView 的框架,使其仅突出显示单元格的一部分(准确地说,我希望 selectionBackroundView 的 X 偏移量为 20 像素)。

有什么简单的方法吗?

更新代码:

UIView *selectionView = [[UIView alloc] init];
[selectionView setBackgroundColor:[UIColor clearColor]];
UIView *selectionSubView = [[UIView alloc] initWithFrame:(CGRectMake(20.0f, 0.0f, 300.0f, 72.0f))];
[selectionSubView setBackgroundColor:[UIColor colorWithRed:(121/255.0) green:(201/255.0) blue:(209/255.0) alpha:1.0]];

UIView *clearView = [[UIView alloc] initWithFrame:(CGRectMake(0.0f, 0.0f, 20.0f, 72.0f))];
[clearView setBackgroundColor: [UIColor clearColor]];

[selectionView addSubview: selectionSubView];
[selectionView addSubview: clearView];

[cell setSelectedBackgroundView: selectionView];

这似乎也不起作用。我在'cellForRowAtIndexPath'中添加了这段代码

提前致谢

【问题讨论】:

  • 有点着急,现在不能编译,但是你试过显式设置selectionView框架吗?因为如果在添加子视图时它没有特定的框架,则结果可能不正确(子视图坐标相对于父视图,但如果父视图没有...)。

标签: iphone ios uitableview


【解决方案1】:

您可以将较小的 UIView 作为您的 selectionView 的子视图并更改该视图的背景颜色。

【讨论】:

    【解决方案2】:

    你可以这样做。

    您为 UIView 创建单独的文件,如下所示。

    TestView.m

    - (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
    }
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
    
    /* Draw a circle */
    // Get the contextRef
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    // Set the border width
    CGContextSetLineWidth(contextRef, 1.0);
    
    // Set the circle fill color to GREEN
    CGContextSetRGBFillColor(contextRef, 100.0, 255.0, 0.0, 1.0);
    
    // Set the cicle border color to BLUE
    CGContextSetRGBStrokeColor(contextRef, 0.0, 0.0, 255.0, 1.0);
    
    // Fill the circle with the fill color
    CGContextFillRect(contextRef, CGRectMake(20, 0, rect.size.width, rect.size.height));
    
    // Draw the circle border
    //CGContextStrokeRectWithWidth(contextRef, rect, 10);//(contextRef, rect);
    }
    

    这个自定义视图可以用作这样的单元格选择的背景视图。

        TestView *bgView = [[TestView alloc] initWithFrame:cell.frame]; // Creating a view for the background...this seems to be required.
    cell.selectedBackgroundView = bgView;
    

    也许对你有帮助。

    谢谢,

    米内什·普罗希特。

    【讨论】:

      【解决方案3】:

      单元格是否有固定大小和高亮区域?

      如果是,则创建一个图像并将图像视图用作 selectedBackgroundView

      【讨论】:

        【解决方案4】:

        尝试为 selectionView 设置帧大小,其中 x = 20。我对此不确定,但我想它应该适用于您的给定场景。

        【讨论】:

          猜你喜欢
          • 2014-08-02
          • 1970-01-01
          • 2011-03-27
          • 2014-05-10
          • 2015-11-24
          • 1970-01-01
          • 1970-01-01
          • 2018-11-25
          • 2020-10-02
          相关资源
          最近更新 更多