集合视图滚动问题

时间:2014-07-16 05:25:05

标签: ios iphone

集合视图滚动问题:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
 cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
        photoFuncVal=1;
size=CGSizeMake(85, 85);
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
UIImageView *icon=(UIImageView *)[cell viewWithTag:100];
dispatch_async(kBgQueue, ^{
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString   stringWithFormat:@"%@",[_responseAdvPicArray objectAtIndex:indexPath.row] ]]];
    if (imgData)
    {
        //image = [UIImage imageWithData:imgData];
        image = [[UIImage alloc] initWithData:imgData];
        //icon.image = image;
        scaledImage=[UIImage imageWithData:UIImageJPEGRepresentation(image, 4.0)];
        scaledImage=[self imageWithImage:image convertToSize:size];
        if (image)
        {
           dispatch_async(dispatch_get_main_queue(), ^{
           //icon.image=scaledImage;
           cell.backgroundColor=[UIColor colorWithPatternImage:scaledImage];

            });
         }
       }
    });

    cell.layer.borderWidth=4;
    cell.layer.borderColor=[[UIColor whiteColor]CGColor];
    return cell;

    }

问题是当滚动集合视图时,图像会重新加载并且有时会重复

2 个答案:

答案 0 :(得分:0)

请试试这个:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
 cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
        photoFuncVal=1;
size=CGSizeMake(85, 85);
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
**cell.backgroundColor = nil;**
UIImageView *icon=(UIImageView *)[cell viewWithTag:100];
dispatch_async(kBgQueue, ^{
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString   stringWithFormat:@"%@",[_responseAdvPicArray objectAtIndex:indexPath.row] ]]];
    if (imgData)
    {
        //image = [UIImage imageWithData:imgData];
        image = [[UIImage alloc] initWithData:imgData];
        //icon.image = image;
        scaledImage=[UIImage imageWithData:UIImageJPEGRepresentation(image, 4.0)];
        scaledImage=[self imageWithImage:image convertToSize:size];
        if (image)
        {
           dispatch_async(dispatch_get_main_queue(), ^{
           //icon.image=scaledImage;
           cell.backgroundColor=[UIColor colorWithPatternImage:scaledImage];

            });
         }
       }
    });

    cell.layer.borderWidth=4;
    cell.layer.borderColor=[[UIColor whiteColor]CGColor];
    return cell;

    }

答案 1 :(得分:0)

像这样注册你的手机:

static NSString *CellIdentifier = @"CollectionCell";

[self.collectionHome registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionCell"];

CollectionViewCell *cell ;
if(cell==nil)
{
    cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
}