2012-04-19 4 views
1

here 질문에서 설명한 것처럼 CAGradientLayer를 사용하여 UITableView의 배경색을 설정하려고했습니다. 나는iOS UITableView : usingCAGradientLayer를 사용하면 초기 개체가 테이블보기에 나타나지 않습니다.

- (void)viewDidLoad 
{ 
    // .... 
    if (!navigationItems) { 
     navigationItems = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil]; 
    } 

} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSArray *colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:213.0f/255.0f green:91.0f/255.0f blue:92.0f/255.0f alpha:1.0f] CGColor][[UIColor colorWithRed:213.0f/255.0f green:0.0f blue:0.0f alpha:1.0f] CGColor], nil]; 
    CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 
    gradientLayer.colors = colors; 
    gradientLayer.frame = tableView.bounds; 

    [tableView.layer insertSublayer:gradientLayer atIndex:0]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    cell.textLabel.text = [navigationItems objectAtIndex:indexPath.row]; 
    cell.textLabel.textColor = [UIColor blackColor]; 

    return cell; 
} 

은 무엇 발생하는 세포가 초기 값으로 내가 설정 한 그라데이션 색상과 중복 된 것으로 나타 채워지는 것입니다 ...이 같은 NSMutableArray를 몇 가지 값으로 jQuery과 채우는하고있다. 해당 셀에는 텍스트/경계선이 표시되지 않습니다 (이 경우 값 1, 2, 3, 4이있는 셀). 여기서 내가 뭘 잘못 했니?

답변

5

backgroundView를 tableView로 설정하면 나를 위해 일했습니다. 여기 내가 어떻게 했어 :

CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 
gradientLayer.colors = colors; 
gradientLayer.frame = tableView.bounds; 

//[tableView.layer insertSublayer:gradientLayer atIndex:0]; 

UIView *tableBackgroundView = [[UIView alloc] init]; 
[tableBackgroundView.layer insertSublayer:gradientLayer atIndex:0]; 
[self.tableView setBackgroundView:tableBackgroundView]; 
+0

이 나를 위해 일한 우수합니다. 고마워요. –

1

조심해 insertSublayer 조심해! 셀을 재활용 할 때마다 새 레이어를 삽입합니다. 따라서 몇 번의 반복 작업을 수행하면 셀 내부에 많은 레이어가 생성됩니다.

이를 방지하는 간단한 루프이 같은 때마다 실행하려면 :

for (CALayer *layer in [cell.barView.layer.sublayers reverseObjectEnumerator]) { 
    if ([layer isKindOfClass:[CAGradientLayer class]]) { 
     [layer removeFromSuperlayer]; 
     break; 
    } 
} 
+0

팁 주셔서 고마워요. – vikmalhotra

관련 문제