1

リストを形成するために動的オブジェクトで埋められるカスタムテーブルビューがあります。セル内の画像の半分の上部にグラデーションオーバーレイを設定しようとしています。

interface.builderでレイアウトを設定しました。

以下は私の質問です:-上下にスクロールした後、グラデーションオーバーレイが毎回再描画されて単色オーバーレイになるという問題があります。誰かがこれを克服する方法を知っていますか?

ありがとう!


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CustomTableViewCell *cell = (CustomTableViewCell* )[tableView dequeueReusableCellWithIdentifier:@"TableCellId"];
    if(cell == nil){
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TableCellId"];
    }

    UIImageView *item_image = (UIImageView *)[cell viewWithTag:@"cell_image"];
    NSString *url_image = [NSString stringWithFormat:@"%@%@", URL_SERVER_HOST, item_pic];

    [item_image setImageWithURL:[NSURL URLWithString:url_image]];

    //Currently get called when new cell is loaded
    UIView *overlay = (UIView *)[cell viewWithTag:@"cell_overlay"];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = overlay.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
    [overlay.layer insertSublayer:gradient atIndex:0];

    UILabel * item_name = (UILabel *)[cell viewWithTag:@"cell_name"];
    item_name.text  = @"test text";

    return cell;
}
4

2 に答える 2

2

グラデーション関連のコードをif (cell == nil)に配置して、セルを再利用するときにグラデーション レイヤーを再度追加しないようにします。

于 2013-03-06T07:32:54.640 に答える
0

グラデーションを適用する前に、オーバーレイ サブレイヤーを nil に設定しました。前の行の下に書くだけです[overlay.layer insertSublayer:gradient atIndex:0];

overlay.layer.sublayers = nil;

于 2016-01-23T11:33:52.647 に答える