我在ViewController中有一个包含double值的数组。我想根据值在tableviewcell中绘制这些值。实际值不应显示为文本。应该用从黄色到红色的颜色值替换,黄色是数组中的最低值,红色是数组中的最高值。 例如:5,10,15,30,20,2,1 应该在细胞中表示为"浅黄色,黄色,橙色,红色,深橙色,浅黄色,非常浅黄色" 我更喜欢小CGRect到gradients.something类似于F3BarGuage。
在下面的代码中,我的数组将是10,11,12,13,14,30,29,28,27,26。(我认为)
我希望对其进行颜色编码,其中30为红色,10为黄色,所有其他值均采用渐变,并在tableCell内的视图中水平显示为背景颜色或子视图(实际上并不重要)。 / p>
没有多少。只是一个只有值的数组。并且不需要任何功能只需将值显示器显示为表格单元格中的颜色。
P.S。我没有尝试任何因为我不知道如何开始。我只改变了背景颜色作为一个整体而不是细分一个细胞然后有颜色
*************代码***************
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];
}
答案 0 :(得分:0)
我最终使用了:
in cellForRowAtindexPath:
for(i = 0; i&lt; [currentRowArray count]; 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];
- (float)transition:(float)value:(float)maximum:(float)start_point:(float)end_point {
return start_point + ((end_point - start_point)*value)/maximum;
}
- (UIColor *)transistion3value:(float)value maximumValue:(float)max {
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];
}