2014-07-17 80 views
0

嗨,大家好,因爲我有一個UITableView與自定義單元格,我解析一些XML數據,使單元格時尚我使用此代碼爲漸變背景,現在的問題是,我的應用程序滯後一次我在tableview中向下或向上滾動。 這是我的代碼:uiscrollview滯後梯度單元格背景

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
(NSIndexPath *)indexPath{  
static NSString *CellIdentifier [email protected]"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

我可以看到你的代碼2個問題:

1)你在主線程下載圖像,你應該這樣做對其他線程。嘗試使用SDWebImage(https://github.com/rs/SDWebImage);

2)您可能想要創建自定義單元格,您在cellForRowAtIndexPath上做了大量工作。嘗試使用UIBezierPath創建陰影並設置yourCell.layer.shouldRasterize = YES

+0

哦,我明白了,我會試試這個謝謝。 – Musashi

+0

可能這2個問題會嚴重影響iphone電池嗎? – Musashi

+0

我不這麼認爲 – Claudio