0

ヒントが必要です。テーブルビューに2つのセルがあります。

最初のものには label.text 値があり、2 としましょう。2 番目のものには 1 の値があります。

合計は 3 (100%)

値をパーセントで表す色でセルを視覚的に表示したいと思います。

したがって、最初のセルは約 67%、2 番目のセルは約 33% になるはずです。

しかし、セルの 67% と 33% を色で塗りつぶすにはどうすればよいでしょうか? 1 つのセルだけに値がある場合、セル全体が色で塗りつぶされます。もう 1 つは空白にする必要があります。

棒グラフのように見せたい。

このような:

ここに画像の説明を入力

4

3 に答える 3

2

できることは、色付きの背景として 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;

これにより、UIView が cell.backgroundView に追加され、塗りつぶしの割合を表すために少し下から始まる色になります。

お役に立てれば

于 2013-10-18T16:25:40.697 に答える
1

使用したい色の割合を計算することができると仮定します。次に、次のように簡単です。

UIView *bg = [[UIView alloc] initWithFrame:frame];
bg.backgroundColor = [UIColor colorWithHue:hue 
                                saturation:percent
                                brightness:brightness
                                     alpha:1.0];
cell.backgroundView = bg;

正確な目標に応じて、彩度と明るさの組み合わせを使用して遊んでみたいと思うかもしれませんが、色相とアルファは一定です。

編集:

リンクに表示されているように、背景を棒グラフにしたい場合は、もう少し複雑な背景ビューを作成する必要があります。

- (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;
}
于 2013-10-19T04:25:52.733 に答える
0

悲しいことに、mashdup の答えは正しくありません。セル全体に色を付けます(Swiftを使用しています)。

編集:

var frame: CGRect = cell.frame
        frame.size.width = frame.size.width*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
        var bg: UIView = UIView(frame: frame)
        bg.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.5)
        cell.addSubview(bg)

Swiftで答えてください。

于 2015-03-08T13:50:44.423 に答える