ios - Gradient Mask is applied multiple times in reused prototype cell -


i have tableview, customer uitableviewcell class - created prototype cell in storyboard/builder.

because cell linked storyboard prototype, reference follows (cellidentifier matches id on prototype cell):

eventslisttableviewcell *cell = (eventslisttableviewcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; 

as such, cell initialised , ready (i can't use "if (cell == nil{...} ")

this fine, want add gradient layer cell, i'm doing within cellforrowatindex:

gradientmask = [cagradientlayer layer]; gradientmask.frame = cell.eventimage.layer.bounds;  gradientmask.startpoint = cgpointmake(0.5, 0.2); gradientmask.endpoint = cgpointmake(0.5, 1.0); gradientmask.colors = [nsarray arraywithobjects:                        (id)[[uicolor colorwithred:0.0f green:0.0f blue:1.0f alpha:0.0f] cgcolor],                         (id)[[uicolor colorwithred:0.0f green:0.0f blue:1.0f alpha:1.0f] cgcolor],nil]; [cell.eventimage.layer insertsublayer:gradientmask atindex:0]; 

the issue here, gradientmask gets applied each re-use of cell, when scroll down gets darker , darker

i realise need apply gradientmask once, when cell first created, i'm not sure call code, never 'init' cell (this handled storyboard)

i have custom class cell, contains properties , no methods?

there various ways of achieving this:

1- property in uitableviewcell subclass

create property in eventslisttableviewcell class hold reference gradientmask:

@interface eventslisttableviewcell : uitableviewcell  @property (weak, nonatomic) cagradientlayer *gradientmask;  @end 

and in cellforrowatindexpath: method:

if (!cell.gradientmask) {     gradientmask = [cagradientlayer layer];     gradientmask.frame = cell.eventimage.layer.bounds;      gradientmask.startpoint = cgpointmake(0.5, 0.2);     gradientmask.endpoint = cgpointmake(0.5, 1.0);     gradientmask.colors = [nsarray arraywithobjects:                            (id)[[uicolor colorwithred:0.0f green:0.0f blue:1.0f alpha:0.0f] cgcolor],                             (id)[[uicolor colorwithred:0.0f green:0.0f blue:1.0f alpha:1.0f] cgcolor],nil];     [cell.eventimage.layer insertsublayer:gradientmask atindex:0];     cell.gradientmask = gradientmask; } 

this make sure gradientmask initialized once.

2- name property of calayer

this way, don't need create property , can handled in cellforrowatindexpath: method itself.

bool gradientfound = no;  (calayer *layer in cell.eventimage.layer.sublayers) {     if ([layer.name isequaltostring:@"gradientlayer"])     {         gradientfound = yes;         break;     } }  if (!gradientfound) {     gradientmask = [cagradientlayer layer];     gradientmask.frame = cell.eventimage.layer.bounds;     gradientmask.name = @"gradientlayer";               //set name     gradientmask.startpoint = cgpointmake(0.5, 0.2);     gradientmask.endpoint = cgpointmake(0.5, 1.0);     gradientmask.colors = [nsarray arraywithobjects:                            (id)[[uicolor colorwithred:0.0f green:0.0f blue:1.0f alpha:0.0f] cgcolor],                             (id)[[uicolor colorwithred:0.0f green:0.0f blue:1.0f alpha:1.0f] cgcolor],nil];     [cell.eventimage.layer insertsublayer:gradientmask atindex:0];  } 

3- declaring gradienlayer in uitableviewcell subclass itself

this cleanest way isolates code related cell within it's class. can initialize cell in awakefromnib method.

@implementation eventslisttableviewcell {     cagradientlayer *gradientmask; }  -(void)awakefromnib {     gradientmask = [cagradientlayer layer];     gradientmask.frame = cell.eventimage.layer.bounds;      gradientmask.startpoint = cgpointmake(0.5, 0.2);     gradientmask.endpoint = cgpointmake(0.5, 1.0);     gradientmask.colors = [nsarray arraywithobjects:                            (id)[[uicolor colorwithred:0.0f green:0.0f blue:1.0f alpha:0.0f] cgcolor],                             (id)[[uicolor colorwithred:0.0f green:0.0f blue:1.0f alpha:1.0f] cgcolor],nil];     [self.eventimage.layer insertsublayer:gradientmask atindex:0]; }  @end 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -