ios - uiscrollview 滞后于渐变单元格背景

标签 ios uitableview

大家好,因为我有一个带有自定义单元格的 UITableView,我在其中解析一些 XML 数据,为了使单元格时尚,我使用此代码作为渐变背景,现在的问题是,一旦我在表格 View 。 这是我的代码:

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

最佳答案

我在您的代码中发现了 2 个问题:

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

2) 您可能想要创建自定义单元格,您在 cellForRowAtIndexPath 上做了很多工作。尝试使用 UIBezierPath 创建阴影并设置 yourCell.layer.shouldRasterize = YES

关于ios - uiscrollview 滞后于渐变单元格背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803054/

相关文章:

ios - 从 UICollectionView 使用 Push Segue 时空白 UITableView

ios - iOS 9 上的 Facebook 分享按钮打开浏览器而不是本地 Facebook 应用程序

ios - 关于 appStoreReceiptURL 的问题

ios - 核心数据支持的 UITableView 在删除单元格时出错

ios - 覆盖-tableView :commitEditingStyle:forRowAtIndexPath: to display info

iphone - 开始输入时 UISearchBar 崩溃

iphone - 在iPhone中使用3G来电的Pjsip在UDP中时没有音频

ios - UICollectionViewFlowLayout 左侧的间隙

ios - 如何使用 ionic 3 从应用商店获取 iOS 部署版本号

iphone - 设置UITableView contentOffset,然后拖动, View 偏移跳转到新位置