2013-08-12 1 views
0

Я не знаю, почему выбранный цвет строки не изменяется в tableview.выбранный цвет фона строки не меняется в ios

Я создал пользовательский элемент и применял градиентный цвет к фону ячейки, а также написал код для изменения 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

Старайтесь избегать использования этого кода в вашем -(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]; 

С каждым вызовом этого метода UIView v будет создан и применяется к вашей камере, что приводит к огромным накладные расходы памяти при прокрутке таблицы.

Что касается вопроса, поскольку вы подклассифицируете 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

не работает, проблема возникает. –