关于UItablecell的CaGradientLayer奇怪的行为

时间:2011-07-17 21:33:31

标签: iphone ios4

我将这个渐变coce插入到我的cellForRowAtIndexPath方法中,但它确实很奇怪。当我继续在页面上下滚动时,它会一遍又一遍地绘制细胞,因此页面会变暗和变暗。我把它放在其他地方吗?

- (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;
}

2 个答案:

答案 0 :(得分:3)

您的代码看起来不完整,但每次表视图请求单元格时都会插入一个图层。

答案 1 :(得分:2)

每次单元格出现在视图中时,您正在调用绘图代码,而您需要这样做的唯一时间是分配和初始化单元格。移动到if语句的括号之间,您不应该再遇到问题。

我知道因为我昨晚做了同样的事......

这适用于您可能想要执行的任何自定义标签或任何其他绘图。除此之外,if语句为标签和背景颜色等任何其他值设置。

祝你好运!