2013-08-12 3 views
0

테이블 뷰에서 선택한 행 색상이 변경되지 않는 이유를 알 수 없습니다.선택한 행의 배경색이 변경되지 않음

셀의 배경에 사용자 지정 셀을 적용하고 그라디언트 색을 적용했으며 선택한 배경을 사용하여 bgcolor를 변경하는 코드를 작성했습니다. 문제는 제가 그라디언트 색상을 사용하지 않았다면 잘 작동하지만 그래디언트 색상을 사용하면 bgcolor가 바뀌지 않는 것입니다.

정확한 문제는 무엇입니까 ?? 이것에 대한 모든 솔루션 ??

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *[email protected]"ViewProfileCell"; 

    MyHomeViewCell *cell= [[MyHomeViewCell alloc] init]; 

    cell=(MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier]; 

    if(!cell) 
    { 
NSArray *nibofMyHomeCell=[[NSBundle mainBundle]loadNibNamed:@"MyHomeViewCell" owner:self options:Nil]; 
     cell=[nibofMyHomeCell objectAtIndex:0]; 


    } 
    UIView *v=[[UIView alloc]init]; 

    v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor]; 

    cell.selectedBackgroundView=v; 

    CAGradientLayer *ViewGradient=[CAGradientLayer layer]; 
    ViewGradient.frame=CGRectMake(0, 0, 320, 52); 
    ViewGradient.colors = [NSArray arrayWithObjects: 
          (id)[[UIColor colorWithRed:27.0f/255.0f green:48.0f/255.0f blue:39.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:26.0f/255.0f green:47.0f/255.0f blue:38.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:25.0f/255.0f green:44.0f/255.0f blue:37.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:23.0f/255.0f green:42.0f/255.0f blue:35.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:22.0f/255.0f green:41.0f/255.0f blue:34.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:22.0f/255.0f green:40.0f/255.0f blue:33.0f/255.0f alpha:1.0f] CGColor], 
          nil]; 

    [cell.layer insertSublayer:ViewGradient atIndex:0]; 
    cell.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
    cell.layer.shadowOpacity=20.0f; 

    cell.layer.borderWidth=1.0f; 

    cell.layer.borderColor=[[UIColor colorWithRed:38.0f/255.0f green:70.0f/255.0f blue:58.0f/255.0f alpha:1.0f] CGColor]; 

}

답변

0

보십시오 따라서 큰 결과 UIView v이 만들어지고 세포에 적용이 메서드 호출 할 때마다 당신의 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 방법

UIView *v=[[UIView alloc]init]; 

    v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor]; 

    cell.selectedBackgroundView=v; 

    CAGradientLayer *ViewGradient=[CAGradientLayer layer]; 
    ViewGradient.frame=CGRectMake(0, 0, 320, 52); 
    ViewGradient.colors = [NSArray arrayWithObjects: 
          (id)[[UIColor colorWithRed:27.0f/255.0f green:48.0f/255.0f blue:39.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:26.0f/255.0f green:47.0f/255.0f blue:38.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:25.0f/255.0f green:44.0f/255.0f blue:37.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:23.0f/255.0f green:42.0f/255.0f blue:35.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:22.0f/255.0f green:41.0f/255.0f blue:34.0f/255.0f alpha:1.0f] CGColor], 
          (id)[[UIColor colorWithRed:22.0f/255.0f green:40.0f/255.0f blue:33.0f/255.0f alpha:1.0f] CGColor], 
          nil]; 

    [cell.layer insertSublayer:ViewGradient atIndex:0]; 
    cell.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
    cell.layer.shadowOpacity=20.0f; 

    cell.layer.borderWidth=1.0f; 

    cell.layer.borderColor=[[UIColor colorWithRed:38.0f/255.0f green:70.0f/255.0f blue:58.0f/255.0f alpha:1.0f] CGColor]; 

에이 코드를 사용하지 않도록합니다 테이블 뷰를 스크롤 할 때 메모리 오버 헤드가 발생합니다.

질문에 대해서는 UITableViewCell을 서브 클래 싱하기 때문에 -(void)drawRect:(CGRect)rect 또는 init에 일반 배경 코드를 구현하십시오. 선택한 배경에 대한 그라데이션이 될 UIView를 만들고 셀 하위 클래스에 적용하십시오.

- (id)init { 
    self = [super init]; 
    if (self) { 
     self.contentView.backgroundColor = [UIColor anyColourYouNeed]; 
     UIView *v=[[UIView alloc]init]; 

     v.layer.backgroundColor=[[UIColor colorWithRed:46.0/255.0f green:139.0/255.0f blue:87.0/255.0f alpha:1 ]CGColor]; 

     cell.selectedBackgroundView=v; 
    } 
    return self; 
} 

호프가 도움이 되길 바랍니다.

+0

동일한 문제가 발생하지 않습니다. –

관련 문제