【问题标题】:How to colorize a cell using x coordinates in iOS如何在 iOS 中使用 x 坐标为单元格着色
【发布时间】:2012-11-28 10:36:53
【问题描述】:

我有一个CustomCell,它是UITableViewCell 的子类。我有一个scrollView。我在 pagingEnabled 模式下使用这个scrollView

我正在使用这段代码来设置这个scrollView的contentSize

scrollView.contentsize = CGSizeMake(720,70)

我想要做的是使用 x 坐标为这个单元格的背景颜色着色。 (5,y) 和 (10,y) 的颜色必须略有不同。我怎样才能做到这一点?

编辑:好吧,我不能直接使用cell.backgroundColor= [UIColor redColor];,因为这会使所有背景变成红色,例如,我需要的是从浅红色开始,以深红色结束。所以,我必须解析所有屏幕并设置适当的颜色。

提前致谢。

【问题讨论】:

    标签: ios uiscrollview uikit uicolor


    【解决方案1】:

    听起来您需要自己绘制背景。最好的解决方案是创建一个与单元格大小相同的屏幕外位图,并在其中进行自定义绘图。然后将表格单元格的背景图像设置为该图像。我相信,如果您使用命名的 UIImage,那么操作系统会为您缓存它并为具有该背景的每个单元重用它。

    如果您需要它更加动态,那么您需要覆盖该表视图行的自定义子类中的 -(void)drawRect: 方法,并即时执行您的自定义绘图。

    【讨论】:

      【解决方案2】:

      在您的 CustomCell 类中实现 UIScrollView 委托函数并将滚动视图委托设置为您的 CustomCell 类。

      然后这样做

      - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
      
          CGPoint point = [scrollView contentOffset];
      
          if (point.x > 80 && point.x < 300) {
              [self setBackgroundColor:[UIColor redColor]];
      
          }else if(point.x > 400 && point.x < 700){
      
              [self setBackgroundColor:[UIColor blueColor]];
          }
      }
      

      【讨论】:

        【解决方案3】:

        检查一下...

           cell.textLabel.textColor=[UIColor colorWithRed:15.0/255.0 green:122.0/255.0 blue:202.0/255.0 alpha:1.0];
        

        这将帮助您为文本着色..

        【讨论】:

        • 我不想给我的文字上色,我想给我的背景颜色上色,无论如何,谢谢
        • 然后稍微改一下...cell.textLabel.backgroundColor = [UIColor clear];或者你也可以这样做 cell.backgroundColor = cell.contentView.backgroundColor;请检查这些..
        • if (indexPath.row % 2 == 0){ cell.backgroundColor =[UIColor blueColor]; } else { cell.backgroundColor =[UIColor whiteColor]; }cell.textLabel.backgroundColor = [UIColor clear]; cell.detailTextLabel.backgroundColor = [UIColor clearColor]; cell.contentView.backgroundColor = [UIColor grayColor]; //你想要哪种颜色 cell.backgroundColor = cell.contentView.backgroundColor;在 cellforrowatindexpath 中添加这些代码
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-21
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        • 2010-12-19
        • 2023-03-20
        相关资源
        最近更新 更多