Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Gradientlayer in cellForRowAtIndexPath
    primarykey
    data
    text
    <p>I have a tableview of custom cells. In each cell, I m adding an label as a subview and loading data in the label.I m adding gradient in my cellForRowAtIndexPath for each cell inside if block where we check whether the cell is nil or not. Its working fine. But, I have an additional requirement now and the problem starts here. I need to check for the values and apply gradient effect for a particular label in the cell. Here is my code.</p> <pre><code>- (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; } </code></pre> <p>Now, the gradient is not applied properly when I reload the table. Its randomly picking up cells and applying gradient. How do I properly remove the gradient before I reload the table. Breaking my head for quite a few days. Please help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload