Ios 将uitableviewcell划分为水平条形图

Ios 将uitableviewcell划分为水平条形图,ios,uitableview,color-codes,Ios,Uitableview,Color Codes,我在ViewController中有一个包含双值的数组。我想根据值在tableviewcell中绘制这些值。实际值不应显示为文本。它应替换为从黄色到红色的颜色值,黄色是数组中的最低值,红色是数组中的最高值。 例:5,10,15,30,20,2,1 应在单元格中表示为“浅黄色、黄色、橙色、红色、深橙色、浅黄色、非常浅黄色” 比起梯度,我更喜欢小的CGRect。类似于F3BarGuage的东西 在下面的代码中,我的数组将是10,11,12,13,14,30,29,28,27,26 我想用30表示红色

我在ViewController中有一个包含双值的数组。我想根据值在tableviewcell中绘制这些值。实际值不应显示为文本。它应替换为从黄色到红色的颜色值,黄色是数组中的最低值,红色是数组中的最高值。 例:5,10,15,30,20,2,1 应在单元格中表示为“浅黄色、黄色、橙色、红色、深橙色、浅黄色、非常浅黄色” 比起梯度,我更喜欢小的CGRect。类似于F3BarGuage的东西

在下面的代码中,我的数组将是10,11,12,13,14,30,29,28,27,26

我想用30表示红色,10表示黄色,所有其他值之间有渐变,并在tableCell内的视图中水平显示它作为背景色或子视图(真的没关系)

没有多少。只是一个只有值的数组。而且不需要任何功能,只需在表格单元格中将估价师显示为颜色即可

另外,我什么都没试过,因为我不知道如何开始。我只改变了背景颜色作为一个整体,没有细分一个细胞,然后有颜色

*************代码***************

TableViewCell.m
@implementation TableViewCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

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

    // Configure the view for the selected state
}

----------------------------------
ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

+ (instancetype) createViewController;

@property (copy,nonatomic) NSMutableArray *values;

@end
--------------------------
static NSString *CellIdentifier = @"CellIdentifier";

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    values = [[NSMutableArray alloc]init];


    for ( int i = 10; i<15; i++){

          [self.values addObject:[NSNumber numberWithDouble:i]];
    }
    for ( int j = 30; j>25; j--){

          [self.values addObject:[NSNumber numberWithDouble:j]];
    }


    UITableView *tableView = (id)[self.view viewWithTag:1];
    tableView.rowHeight = 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
 */


// ******************** Class Methods ********************
#pragma mark - Class Methods

+ (instancetype) createViewController {
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    return [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];

}

#pragma mark- Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

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

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.valueLabel.text = [values componentsJoinedByString:@" , "];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
    [tableView deselectRowAtIndexPath:newIndexPath animated:YES];
} 
TableViewCell.m
@实现TableViewCell
-(id)initWithStyle:(UITableViewCellStyle)样式重用标识符:(NSString*)重用标识符
{
self=[super-initWithStyle:style-reuseIdentifier:reuseIdentifier];
如果(自我){
//初始化代码
}
回归自我;
}
-(无效)从NIB中唤醒{
//初始化代码
}
-(无效)设置选定:(BOOL)选定动画:(BOOL)动画{
[超级设置选定:选定动画:动画];
//为所选状态配置视图
}
----------------------------------
ViewController.h
#进口
@界面ViewController:UIViewController
+(instancetype)createViewController;
@属性(复制,非原子)NSMutableArray*值;
@结束
--------------------------
静态NSString*CellIdentifier=@“CellIdentifier”;
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后执行任何其他设置。
值=[[NSMUTABLEARRY alloc]init];
对于(int i=10;i25;j--){
[self.values addObject:[NSNumber numberwhithdouble:j]];
}
UITableView*tableView=(id)[self.view viewWithTag:1];
tableView.rowHeight=50;
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
/*
#pragma标记-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方{
//使用[segue destinationViewController]获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
//**********************类方法********************
#pragma标记类方法
+(instancetype)createViewController{
UIStoryboard*情节提要=[UIStoryboard情节提要,名称:@“Main”bundle:nil];
返回[storyboard InstanceEviewController标识符:@“ViewController”];
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回5;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
TableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.valueLabel.text=[values componentsJoinedByString:@“,”];
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)newIndexPath{
[tableView取消行索引路径:newIndexPath动画:是];
} 
我最终使用了:

在cellForRowAtindexPath中:


对于(i=0;i请分享您尝试过的内容和不起作用的内容。
    UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, 25, 35)];
    currentValue = [currentRowArray objectAtIndex:i];
    temp.text = [currentValue stringValue];
    temp.backgroundColor = [self transistion3value:[currentValue doubleValue] maximumValue:maxValue]; // maxValue is a double indicating the maximum at this row
    [cell.View addSubview:temp];
return start_point + ((end_point - start_point)*value)/maximum;
float s1,s2,s3,e1,e2,e3,r1,r2,r3;

s1=0; s2=255; s3=0.0; // start - green color
e1=255.0; e2=0.0; e3=0.0; // end - red color

r1=[self transition:value :max :s1 :e1];
r2=[self transition:value :max :s2 :e2];
r3=[self transition:value :max :s3 :e3];

return [UIColor colorWithRed:r1/255.0  green:r2/255.0 blue:r3/255.0 alpha:1];