[Résolu] gradient d'une cell ne s'affiche pas au lancement de l'appli

LouLou Membre
février 2015 modifié dans API UIKit #1

Salut,


 


 


SOLUTION: eh bien c'était vraiment "tricky". Il fallait utiliser la méthode :



MyRealCell* cell = (MyRealCell*)[tableView dequeueReusableCellWithIdentifier:@MyCell forIndexPath:indexPath];

avec forIndexPath pour que la frame de la cell ne soit pas à  0,0,0,0. Au lieu de :



MyRealCell* cell = (MyRealCell*)[tableView dequeueReusableCellWithIdentifier:@MyCell];//la frame retourne 0,0,0,0

___


EDIT: en fait, je dois préciser ma question : quand le splitViewController s'affiche au lancement, les cells n'ont pas le dégradé. En revanche, quand j'ajoute une cell, le tableView est reloadé, et les cells s'affichent avec le bon dégradé. J'ai tenté de rajouter layoutIfNeeded ou setNeedsLayout sur la cell, mais au démarrage, la cell a un background blanc, au lieu d'avoir le degradé. Sauriez-vous pourquoi?


 


EDIT #2 : il semble qu'après un drag, quand la cell sort de l'écran, elle est bien actualisée. Mais au démarrage, au lancement de l'appli, les cells ne veulent pas prendre les réglages. (J'ai tenté avec une custom cell également).



@interface UIView (Gradient)
-(void) addLinearUniformGradient:(NSArray *)stopColors;
@end


@implementation UIView (Gradient)
-(void) addLinearUniformGradient:(NSArray *)stopColors
{
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = stopColors;
gradient.startPoint = CGPointMake(0.5f, 0.0f);
gradient.endPoint = CGPointMake(0.5f, 1.0f);
[self.layer addSublayer:gradient];
}
@end


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
[cell.layer setCornerRadius:7.0f];
[cell.layer setMasksToBounds:YES];
[cell.layer setBorderWidth:2.0f];

// gradient

CGRect backgroundViewFrame = cell.contentView.frame;
backgroundViewFrame.size.height = cell.contentView.frame.size.height;
cell.backgroundView = [[UIView alloc] initWithFrame:backgroundViewFrame];
[cell.backgroundView addLinearUniformGradient:[NSArray arrayWithObjects:
(id)[[UIColor redColor] CGColor],
(id)[[UIColor greenColor] CGColor], nil]];


/*
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor]CGColor], (id)[[UIColor redColor]CGColor], nil];
//[cell.layer addSublayer:gradient];//marche pas
[cell.contentView.layer addSublayer:gradient];//marche pas


CGRect backgroundViewFrame = cell.contentView.frame;
backgroundViewFrame.size.height = cell.contentView.frame.size.height;
UIView* grad = [[UIView alloc] initWithFrame:backgroundViewFrame];
[grad addLinearUniformGradient:[NSArray arrayWithObjects:
(id)[[UIColor redColor] CGColor],
(id)[[UIColor greenColor] CGColor], nil]];
[cell.contentView addSubview:grad];//marche pas

[cell.contentView setBackgroundColor:[UIColor redColor]];//ca marche
*/
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@CellDetail];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

Connectez-vous ou Inscrivez-vous pour répondre.