Spring
Spring

Reputation: 11855

CaGradientLayer on UItablecell weird behaviour

I inserted this gradient coce into my cellForRowAtIndexPath method, but it does weird things. when I continuesly scroll up and down the page, it paints over and over the cells, so page gets darker and darker. Shold I put this somewhere else?

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

    //NSString *cellValue=[variable object ]
    // Configure the cell...

NSString *cellValue = [items objectAtIndex:indexPath.row];

if (indexPath.row==0){


    cell.textLabel.text=cellValue;
        //cell.textLabel.font=bold;
    cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:11];

    cell.textLabel.font = [UIFont boldSystemFontOfSize:(CGFloat)16];


}
else {

    cell.textLabel.text=cellValue;
        //cell.textLabel.font=bold;
    cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:14];

}

UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
UIColor *color = [UIColor colorWithHue:0.56f  saturation:0.98 brightness:0.65 alpha:0.5];
UIColor *colorWhite = [UIColor colorWithHue:0.0f  saturation:0.0 brightness:0.95 alpha:0.5];
UIColor *colorBlack = [UIColor colorWithHue:1  saturation:1 brightness:0 alpha:0.2];


myBackView.backgroundColor=color;
UIColor *textColor = [UIColor colorWithHue:0.0f  saturation:0.0 brightness:0.95 alpha:1];

cell.selectedBackgroundView = myBackView;
cell.selectedTextColor=textColor;
[myBackView autorelease];


UIView *cellView = [[UIView alloc] initWithFrame:cell.frame];
                CAGradientLayer *gradient = [CAGradientLayer layer];
                gradient.frame = cellView.bounds;
                gradient.startPoint = CGPointMake(0, 0.5);
                gradient.endPoint = CGPointMake(1.0, 0.5);
                gradient.colors = [NSArray arrayWithObjects:(id)[colorBlack CGColor], (id)[colorWhite CGColor], nil];
                [cell.layer insertSublayer:gradient atIndex:0];

[cellView autorelease];


return cell;
}

Upvotes: 0

Views: 257

Answers (2)

SushiGrass Jacob
SushiGrass Jacob

Reputation: 19834

You are calling the drawing code each time the cell appears in the view when the only time you need to do so is when it is allocated and init'ed. Move to between the brackets of the if statement and you shouldn't have a problem any more.

I know because I did the same thing last night…

This goes for any custom labels or any other drawing that you might want to do. Outside of that if statement goes any value setting for anything else like labels and background colors.

Good luck!

Upvotes: 2

Marcelo Alves
Marcelo Alves

Reputation: 1846

Your code looks like isn't complete, but you're inserting a layer EVERY time the table view requests a cell.

Upvotes: 3

Related Questions