uiscrollview滞后与渐变细胞背景

时间:2014-07-17 12:04:43

标签: ios uitableview

大家好,因为我有一个带有自定义单元格的UITableView,我解析了一些XML数据,为了使单元格时尚我使用此代码进行渐变背景,现在问题是我的应用程序滞后一旦我向下滚动或向上滚动实现代码如下。 这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  
(NSIndexPath *)indexPath{    
static NSString *CellIdentifier =@"ProvidersCell";
ProvidersViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[ProvidersViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier];       
}
NSString*imageurl = [XMLReader getValue:[[providers objectAtIndex:indexPath.row] objectForKey: @"Image"]];
if ([imageurl  isEqual: @""]) {
    UIImage* myImage = [UIImage imageWithData:
                        [NSData dataWithContentsOfURL:
                         [NSURL URLWithString:@"http://www./nopic.jpg"]]];

    cell.provider_img.image=myImage;
}else{
    UIImage* myImage = [UIImage imageWithData:
                        [NSData dataWithContentsOfURL:
                         [NSURL URLWithString:imageurl]]];

    cell.provider_img.image=myImage;
}
    [cell setBackgroundColor:[UIColor clearColor]];

    CAGradientLayer *grad = [CAGradientLayer layer];
    grad.frame = cell.bounds;
    grad.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:241/255.0 green:241/255.0 blue:242/255.0 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:231/255.0 green:231/255.0 blue:232/255.0 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:207/255.0 green:207/255.0 blue:207/255.0 alpha:1.0] CGColor], nil];

    [cell setBackgroundView:[[UIView alloc] init]];
    [cell.backgroundView.layer insertSublayer:grad atIndex:0];

    CAGradientLayer *selectedGrad = [CAGradientLayer layer];
    selectedGrad.frame = cell.bounds;
    selectedGrad.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:24/255.0 green:215/255.0 blue:229/255.0 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:22/255.0 green:206/255.0 blue:219/255.0 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:9/255.0 green:173/255.0 blue:185/255.0 alpha:1.0] CGColor], nil];

    [cell setSelectedBackgroundView:[[UIView alloc] init]];
    [cell.selectedBackgroundView.layer insertSublayer:selectedGrad atIndex:0];


cell.provider_name.text =[XMLReader getValue:[[providers objectAtIndex:indexPath.row] objectForKey: @"Name"]];
cell.provider_condition.text = [XMLReader getValue:[[providers objectAtIndex:indexPath.row] objectForKey: @"Condition"]];
cell.provider_payout.text = [XMLReader getValue:[[providers objectAtIndex:indexPath.row] objectForKey: @"Payout"]];
NSString*rating = [XMLReader getValue:[[providers objectAtIndex:indexPath.row]objectForKey:@"Rating"]];
NSInteger rate = [rating integerValue];

return cell;}

1 个答案:

答案 0 :(得分:1)

我的代码可以看到2个问题:

1)您正在主线程上下载图像,您应该在其他线程上进行。尝试使用SDWebImage(https://github.com/rs/SDWebImage);

2)您可能想要创建自定义单元格,您正在对cellForRowAtIndexPath进行大量工作。尝试使用UIBezierPath创建阴影并设置yourCell.layer.shouldRasterize = YES