0

我不知道表格视图中所选行颜色没有变化的原因。

我创建了自定义单元格并将渐变颜色应用于单元格的背景,还编写了代码以使用选定的背景更改 bgcolor。问题是,如果我不使用渐变颜色,它工作得很好,但如果使用渐变颜色,它不会改变 bgcolor。

具体问题是什么??有什么解决办法吗??

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

    static NSString *cellidentifier=@"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];

}

4

1 回答 1

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都会创建并应用于您的单元格,因此在滚动 tableview 时会导致巨大的内存开销。

至于问题,因为您是UITableViewCell的子类,所以-(void)drawRect:(CGRect)rect在init中或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;
}

希望这有帮助,干杯!

于 2013-08-12T22:08:22.397 回答