Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何用百分比颜色填充UITableViewCell_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 如何用百分比颜色填充UITableViewCell

Ios 如何用百分比颜色填充UITableViewCell,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我需要一些小费。 我在tableview中有两个单元格 第一个有一个label.text值,比如说2。 第二个值为1 总数为3(100%) 我想用一种颜色直观地显示单元格,该颜色以百分比表示值 所以第一个细胞应该是67%,第二个细胞应该是33% 但是我如何用颜色填充67%和33%的单元格呢?如果只有一个单元格有一个值,那么整个单元格将填充颜色。另一个应该是空白的 我希望它们看起来像一个柱状图 像这样: 您可以做的是添加一个UIView作为带有颜色的背景。但将视图的高度更改为单元格高度的百分比 C

我需要一些小费。 我在tableview中有两个单元格

第一个有一个label.text值,比如说2。 第二个值为1

总数为3(100%)

我想用一种颜色直观地显示单元格,该颜色以百分比表示值

所以第一个细胞应该是67%,第二个细胞应该是33%

但是我如何用颜色填充67%和33%的单元格呢?如果只有一个单元格有一个值,那么整个单元格将填充颜色。另一个应该是空白的

我希望它们看起来像一个柱状图

像这样:


您可以做的是添加一个UIView作为带有颜色的背景。但将视图的高度更改为单元格高度的百分比

CGRect frame = cell.frame;
frame.size.height = frame.size.height*0.66; //The 0.66 is the percentage as a decimal
frame.origin.y = cell.frame.size.height - frame.size.height; //adjust where it should start
UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor redColor];
cell.backgroundView = bg;
这将为cell.backgroundView添加一个UIView,颜色从稍低的位置开始,以表示填充百分比


希望这对你有所帮助,你可以做的是添加一个UIView作为背景,并添加颜色。但将视图的高度更改为单元格高度的百分比

CGRect frame = cell.frame;
frame.size.height = frame.size.height*0.66; //The 0.66 is the percentage as a decimal
frame.origin.y = cell.frame.size.height - frame.size.height; //adjust where it should start
UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor redColor];
cell.backgroundView = bg;
这将为cell.backgroundView添加一个UIView,颜色从稍低的位置开始,以表示填充百分比


希望这对你有所帮助,你可以做的是添加一个UIView作为背景,并添加颜色。但将视图的高度更改为单元格高度的百分比

CGRect frame = cell.frame;
frame.size.height = frame.size.height*0.66; //The 0.66 is the percentage as a decimal
frame.origin.y = cell.frame.size.height - frame.size.height; //adjust where it should start
UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor redColor];
cell.backgroundView = bg;
这将为cell.backgroundView添加一个UIView,颜色从稍低的位置开始,以表示填充百分比


希望这对你有所帮助,你可以做的是添加一个UIView作为背景,并添加颜色。但将视图的高度更改为单元格高度的百分比

CGRect frame = cell.frame;
frame.size.height = frame.size.height*0.66; //The 0.66 is the percentage as a decimal
frame.origin.y = cell.frame.size.height - frame.size.height; //adjust where it should start
UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor redColor];
cell.backgroundView = bg;
这将为cell.backgroundView添加一个UIView,颜色从稍低的位置开始,以表示填充百分比


希望这对你有所帮助,我假设你可以计算出你想要使用的颜色的百分比。那么就很容易做到:

UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor colorWithHue:hue 
                                saturation:percent
                                brightness:brightness
                                     alpha:1.0];
cell.backgroundView = bg;
根据您的具体目标,您可能希望使用饱和度和亮度的组合,但使用恒定的色调和alpha

编辑:

如果希望背景为条形图(如链接中所示),则需要创建稍微复杂的背景视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const CellIdentifier = @"MyCellIdentifier";
    static const NSInteger BarTag = 0xBEEF;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                            forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
        UIView *background = [[UIView alloc] initWithFrame:cell.bounds];
        background.autoresizeMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        UIView *bar = [[UIView alloc] initWithFrame:cell.bounds];
        bar.autoresizeMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
        bar.tag = BarTag;
        bar.backgroundColor = [UIColor redColor];
        [background addSubView:bar];
        cell.backgroundView = background;
    }

    CGFloat percent = .5; // Calculate this somehow
    UIView *backgroundBar = [cell.backgroundView viewWithTag:BarTag];
    CGFrame frame = backgroundBar.frame;
    frame.size.width = CGRectGetWidth(tableView.frame) * percent;
    backgroundBar.frame = frame;

    return cell;
}

我假设你可以计算出你想要使用的颜色的百分比。那么就很容易做到:

UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor colorWithHue:hue 
                                saturation:percent
                                brightness:brightness
                                     alpha:1.0];
cell.backgroundView = bg;
根据您的具体目标,您可能希望使用饱和度和亮度的组合,但使用恒定的色调和alpha

编辑:

如果希望背景为条形图(如链接中所示),则需要创建稍微复杂的背景视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const CellIdentifier = @"MyCellIdentifier";
    static const NSInteger BarTag = 0xBEEF;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                            forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
        UIView *background = [[UIView alloc] initWithFrame:cell.bounds];
        background.autoresizeMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        UIView *bar = [[UIView alloc] initWithFrame:cell.bounds];
        bar.autoresizeMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
        bar.tag = BarTag;
        bar.backgroundColor = [UIColor redColor];
        [background addSubView:bar];
        cell.backgroundView = background;
    }

    CGFloat percent = .5; // Calculate this somehow
    UIView *backgroundBar = [cell.backgroundView viewWithTag:BarTag];
    CGFrame frame = backgroundBar.frame;
    frame.size.width = CGRectGetWidth(tableView.frame) * percent;
    backgroundBar.frame = frame;

    return cell;
}

我假设你可以计算出你想要使用的颜色的百分比。那么就很容易做到:

UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor colorWithHue:hue 
                                saturation:percent
                                brightness:brightness
                                     alpha:1.0];
cell.backgroundView = bg;
根据您的具体目标,您可能希望使用饱和度和亮度的组合,但使用恒定的色调和alpha

编辑:

如果希望背景为条形图(如链接中所示),则需要创建稍微复杂的背景视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const CellIdentifier = @"MyCellIdentifier";
    static const NSInteger BarTag = 0xBEEF;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                            forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
        UIView *background = [[UIView alloc] initWithFrame:cell.bounds];
        background.autoresizeMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        UIView *bar = [[UIView alloc] initWithFrame:cell.bounds];
        bar.autoresizeMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
        bar.tag = BarTag;
        bar.backgroundColor = [UIColor redColor];
        [background addSubView:bar];
        cell.backgroundView = background;
    }

    CGFloat percent = .5; // Calculate this somehow
    UIView *backgroundBar = [cell.backgroundView viewWithTag:BarTag];
    CGFrame frame = backgroundBar.frame;
    frame.size.width = CGRectGetWidth(tableView.frame) * percent;
    backgroundBar.frame = frame;

    return cell;
}

我假设你可以计算出你想要使用的颜色的百分比。那么就很容易做到:

UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor colorWithHue:hue 
                                saturation:percent
                                brightness:brightness
                                     alpha:1.0];
cell.backgroundView = bg;
根据您的具体目标,您可能希望使用饱和度和亮度的组合,但使用恒定的色调和alpha

编辑:

如果希望背景为条形图(如链接中所示),则需要创建稍微复杂的背景视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const CellIdentifier = @"MyCellIdentifier";
    static const NSInteger BarTag = 0xBEEF;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                            forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
        UIView *background = [[UIView alloc] initWithFrame:cell.bounds];
        background.autoresizeMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        UIView *bar = [[UIView alloc] initWithFrame:cell.bounds];
        bar.autoresizeMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
        bar.tag = BarTag;
        bar.backgroundColor = [UIColor redColor];
        [background addSubView:bar];
        cell.backgroundView = background;
    }

    CGFloat percent = .5; // Calculate this somehow
    UIView *backgroundBar = [cell.backgroundView viewWithTag:BarTag];
    CGFrame frame = backgroundBar.frame;
    frame.size.width = CGRectGetWidth(tableView.frame) * percent;
    backgroundBar.frame = frame;

    return cell;
}

不幸的是,mashdup的答案是不正确的。它为整个单元格着色(我使用的是Swift)

编辑:


用Swift回答。

不幸的是,mashdup的回答不正确。它为整个单元格着色(我使用的是Swift)

编辑:


用Swift回答。

不幸的是,mashdup的回答不正确。它为整个单元格着色(我使用的是Swift)

编辑:


用Swift回答。

不幸的是,mashdup的回答不正确。它为整个单元格着色(我使用的是Swift)

编辑:



迅速回答。

很好。但我的整个牢房都是红色的,像这样的很好。但我的整个牢房都是红色的,像这样的很好。但我的整个牢房都是红色的,像这样的很好。但我的整个细胞都是红色的。像这样的东西只会给我一种“定制”的颜色。我想制作一个像问题(参考链接)中的示例一样的单元格。颜色的类型不相关。需要背景视图的宽度大小。你说得对。我不明白他的意图。我会很快更新答案。谢谢。我还将注意到,我在代码中展示的自动resizemask是整个过程的关键。如果它们是错误的,那么在第一次填充表时,大小将是错误的。这只会给我一个“自定义”颜色。我想制作一个像问题(参考链接)中的示例一样的单元格。颜色的类型不相关。需要背景视图的宽度大小。你说得对。我不明白他的意图。我会很快更新答案。谢谢。我还将注意到,我在代码中展示的自动resizemask是整个过程的关键。如果它们是错误的,那么在第一次填充表时,大小将是错误的。这只会给我一个“自定义”颜色。我想制作一个像问题(参考链接)中的示例一样的单元格。颜色的类型不相关。需要背景视图的宽度大小。你说得对。我不明白他的意图。我会很快更新答案。谢谢。我还将注意到,我在代码中展示的自动resizemask是整个过程的关键。如果它们是错误的,那么在第一次填充表时,大小将是错误的。这只会给我一个“自定义”颜色。我想制作一个像问题(参考链接)中的示例一样的单元格。颜色的类型不相关。需要背景视图的宽度大小。你说得对。我不明白他的意图。我会更新答案的