0

カスタムセルのテーブルビューがあります。各セルで、サブビューとしてラベルを追加し、ラベルにデータをロードしています。セルが nil かどうかをチェックする if ブロック内の各セルの cellForRowAtIndexPath にグラデーションを追加しています。その正常に動作します。しかし、追加の要件があり、問題はここから始まります。値を確認し、セル内の特定のラベルにグラデーション効果を適用する必要があります。これが私のコードです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   static NSString *CellIdentifier = @"Cell";
   NSNumberFormatter *formatter = [NSNumberFormatter new];
   [formatter setNumberStyle:NSNumberFormatterDecimalStyle];

    HistoryCustomCell *cell = (HistoryCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
             cell = [[HistoryCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

            CAGradientLayer *gradient = [CAGradientLayer layer];
            gradient.frame = CGRectMake(cell.frame.origin.x + 2, cell.frame.origin.y, tableView.frame.size.width - 3, 44.0);
            gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:228.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:254.0/255.0 green:254.0/255.0 blue:254.0/255.0 alpha:1]CGColor], nil];
            [cell.layer insertSublayer:gradient atIndex:0];

       }

        NSMutableDictionary *dictValue = [arrHistoryValues objectAtIndex:indexPath.row];            

           tempBestEverValue = [dictValue objectForKey:@"bestEverValue"];
           tempBestValue = [dictValue objectForKey:@"bestValue"];

            cell.cell1.text = [dictValue objectForKey:@"year"];

            NSString *janValue = [dictValue objectForKey:@"janValue"];
            if ([janValue isEqualToString:@"(null)"]) {
                janValue = @"";
                cell.cell2.text = [NSString stringWithFormat:@"%@",janValue];

            }
            else
            {
                janValue = [formatter stringFromNumber:[NSNumber numberWithInteger:[janValue intValue]]];
                cell.cell2.text = [NSString stringWithFormat:@"$%@",janValue];

            }

            if ([[dictValue objectForKey:@"janBest"] isEqualToString:[dictValue objectForKey:@"janValue"]])
            {
                cell.cell2.font = [UIFont boldSystemFontOfSize:12.0];
            }
             if ([[dictValue objectForKey:@"janValue"]intValue] == [tempBestEverValue intValue])

            {
                CAGradientLayer *gradient = [CAGradientLayer layer];
                gradient.frame = cell.cell2.frame;
                gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:165.0 green:239.0 blue:156.0 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:140.0 green:203.0 blue:90.0 alpha:1.0] CGColor], nil];
                [cell.layer insertSublayer:gradient atIndex:1];
            }
            else if ([[dictValue objectForKey:@"janValue"]intValue] == [tempBestValue intValue])

            {
                CAGradientLayer *gradient = [CAGradientLayer layer];
                gradient.frame = cell.cell2.frame;
                gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:173.0 green:251.0 blue:173.0 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:181.0 green:255.0 blue:181.0 alpha:1.0] CGColor], nil];
                [cell.layer insertSublayer:gradient atIndex:1];
            }



            NSString *febValue = [dictValue objectForKey:@"febValue"];
            if ([febValue isEqualToString:@"(null)"]) {
                febValue = @"";
                cell.cell3.text = [NSString stringWithFormat:@"%@",febValue];

            }
            else
            {
                febValue = [formatter stringFromNumber:[NSNumber numberWithInteger:[febValue intValue]]];
                cell.cell3.text = [NSString stringWithFormat:@"$%@",febValue];

            }

            if ([[dictValue objectForKey:@"febValue"]intValue] == [tempBestEverValue intValue])

            {
                CAGradientLayer *gradient = [CAGradientLayer layer];
                gradient.frame = cell.cell3.frame;
                gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:165.0/255.0 green:239.0/255.0 blue:156.0/255.0 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:140.0/255.0 green:203.0/255.0 blue:90.0/255.0 alpha:1.0] CGColor], nil];
                [cell.layer insertSublayer:gradient atIndex:1];
            }
            else if ([[dictValue objectForKey:@"febValue"]intValue] == [tempBestValue intValue])

            {

                CAGradientLayer *gradient = [CAGradientLayer layer];
                gradient.frame = cell.cell3.frame;
                gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:173.0/255.0 green:251.0/255.0 blue:173.0/255.0 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:181.0/255.0 green:255.0/255.0 blue:181.0/255.0 alpha:1.0] CGColor], nil];
                [cell.layer insertSublayer:gradient atIndex:1];
            }
            if ([[dictValue objectForKey:@"febBest"] isEqualToString:[dictValue objectForKey:@"febValue"]])
            {
                cell.cell3.font = [UIFont boldSystemFontOfSize:12.0];
            }

      return cell;
}

現在、テーブルをリロードするとグラデーションが正しく適用されません。ランダムにセルをピックアップし、グラデーションを適用します。テーブルをリロードする前にグラデーションを適切に削除するにはどうすればよいですか? かなりの数日間私の頭を壊しました。助けてください。

4

2 に答える 2

0

既にカスタム セル ( HistoryCustomCell) があります。もう少しうまく使用してください。

グラデーション レイヤーをテーブル デリゲートに追加するのではなく、カスタム クラスに追加して公開します。これで、デリゲートで次のことができます。

cell.gradientLayer.opacity = 0;

グラデーションが不要で、次の場合:

cell.gradientLayer.opacity = 1;
cell.gradientLayercolors = ...;

グラデーションが必要で、色を指定する必要がある場合。

于 2013-06-10T15:32:36.743 に答える
0

再利用可能な細胞がどのように機能するかを考慮していないと思います。ある時点で、特定のセルにグラデーション レイヤーを追加します[cell.layer insertSublayer:gradient atIndex:1];。このセルを表示領域の外にスクロールすると、このセルは後で他の行に再利用され、まだレイヤーが含まれています。

次のことを試してください: グラデーション背景なしでセルを表示する場合は、メソッドが返すセルにグラデーション レイヤーが含まれていないことを確認してください。2 つのケースに 2 つの異なるセル識別子を使用します。たとえば@"CellIdentifier"@"GradientCellIdentifier"後者の場合、セルの初期化時にのみグラデーション レイヤーを追加します。

于 2013-06-10T15:27:13.913 に答える