사용자 정의 셀에 대한 테이블 뷰가 있습니다. 각 셀에서 하위 뷰로 레이블을 추가하고 label에 데이터를로드합니다. 셀이 nil인지 여부를 확인하는 블록 인 경우 각 셀의 내 cellForRowAtIndexPath에 그라디언트를 추가합니다. 잘 작동합니다. 하지만 지금은 추가 요구 사항이 있으며 여기에서 문제가 시작됩니다. 값을 확인하고 셀의 특정 레이블에 그라데이션 효과를 적용해야합니다. 여기 내 코드가있다.cellForRowAtIndexPath에 Gradientlayer 추가
- (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;
}
이제 테이블을 다시로드 할 때 그래디언트가 제대로 적용되지 않습니다. 무작위로 세포를 집어 들고 그라디언트를 적용합니다. 테이블을 다시로드하기 전에 어떻게 그래디언트를 제대로 제거해야합니까? 꽤 오래 며칠 동안 내 머리를 깨뜨렸다. 도와주세요.
여기 항상 셀에 그라디언트가 있어야합니다. 그리고 일부 가치에 대해서는 다른 그라디언트 색상을 적용해야합니다. 그 이유는, 모든 셀에 대해 [cell.layer insertSublayer : gradient atIndex : 0]을 설정하고 다른 그라데이션 색상을 표시해야하는 특정 열 셀에 대해 [cell.layer insertSublayer : gradient atIndex : 1]을 설정했기 때문입니다. –
실제로 셀 행을 재사용하지 않는다면, tableview가 scrollview에 추가됩니다. 그래서 모든 세포가 항상 보입니다. –