【问题标题】:Adding Gradientlayer in cellForRowAtIndexPath在 cellForRowAtIndexPath 中添加渐变层
【发布时间】:2013-06-10 15:12:16
【问题描述】:

我有一个自定义单元格的表格视图。在每个单元格中,我添加一个标签作为子视图并在标签中加载数据。我在我的 cellForRowAtIndexPath 中为 if 块内的每个单元格添加渐变,我们检查单元格是否为 nil。它工作正常。但是,我现在有一个额外的要求,问题从这里开始。我需要检查值并为单元格中的特定标签应用渐变效果。这是我的代码。

- (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;
}

现在,当我重新加载表格时,渐变没有正确应用。它随机拾取单元格并应用渐变。在重新加载表格之前如何正确删除渐变。头疼了好几天。请帮忙。

【问题讨论】:

    标签: ios gradient tableviewcell cagradientlayer


    【解决方案1】:

    我认为您没有考虑可重复使用单元的工作原理。在某些时候,您将渐变层添加到给定的单元格:[cell.layer insertSublayer:gradient atIndex:1];。如果您将此单元格滚动到可见区域之外,则此单元格稍后会被其他行重用,仍然包含该图层。

    尝试以下操作:当您想要显示没有渐变背景的单元格时,请确保您的方法返回的单元格不包含渐变层。对这两种情况使用两个不同的小区标识符,例如@"CellIdentifier"@"GradientCellIdentifier" 并在后一种情况下仅在单元格初始化时添加渐变层。

    【讨论】:

    • 在这里,我一直希望单元格呈现渐变。在此基础上,对于某些值,我需要应用其他渐变颜色。这就是原因,我为所有单元格设置了 [cell.layer insertSublayer:gradient atIndex:0],为需要显示不同渐变颜色的特定列单元格设置了 [cell.layer insertSublayer:gradient atIndex:1]。
    • 我实际上并没有重用单元格行,表格视图被添加到滚动视图中。因此所有单元格将始终可见。
    【解决方案2】:

    您已经有一个自定义单元格 (HistoryCustomCell),使用它更好一点。

    不要在表格委托中添加渐变层,而是将其添加到自定义类中并使其公开可用。现在,您可以在委托中执行以下操作:

    cell.gradientLayer.opacity = 0;
    

    当不需要渐变时:

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

    当需要渐变并且需要指定颜色时。

    【讨论】:

    • 我一直想为整个单元格显示渐变颜色,然后我需要为特定值显示其他渐变颜色。
    • 没关系。自定义单元格可以具有完整背景的渐变和背景部分的渐变,并使它们都公开可用。同样的过程也适用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 2017-03-17
    • 2012-11-19
    • 1970-01-01
    相关资源
    最近更新 更多