iOS:UICollectionView Cell加载非常不稳定

时间:2016-02-23 07:10:03

标签: ios objective-c uicollectionview uicollectionviewcell sdwebimage

我使用UICollectionView显示使用SDWebImage的图像列表,这是非常生涩并且滚动不流畅。关于如何处理此事的任何建议?

下面的代码是

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString * cellIdentifier = @"offersCell";

    HotDealsCollectionViewCell * cell = (HotDealsCollectionViewCell *)[self.productsCollectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

    cell.layer.shadowColor = [[UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0] CGColor];
    cell.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
    cell.layer.shadowRadius = 3.0f;
    cell.layer.shadowOpacity = 1.0f;
    cell.layer.masksToBounds = NO;
    CGRect shadowFrame = cell.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
    cell.layer.shadowPath = shadowPath;

    cell.backgroundColor = [UIColor whiteColor];

    if ([self.productsArray count] > 0) {
        Product * product = [self.productsArray objectAtIndex:indexPath.item];
        cell.productName.text = product.productName;
        cell.productEndDate.hidden = NO;
        cell.productImageView.clipsToBounds = YES;
        cell.productEndDate.textAlignment = NSTextAlignmentCenter;

           [cell.productImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", product.productImage]] placeholderImage:[UIImage imageNamed:@"noProduct"]];


    }

return cell;
}

3 个答案:

答案 0 :(得分:1)

由于图像分配,请尝试以下

module.exports = {
   makeUser: function(username, password){
    return new Promise(function(resolve, reject) {
      bcrypt.genSalt(10, function(err, salt){
        bcrypt.hash(password, salt, null, function(err, hash) {
          if (err) {
            console.log("hashing the password failed, see user.js " + err);
            reject(err);
          }
          else {
            console.log("hash was successful.");
            resolve(hash);
          }
        })
      })
    })
    .then(function(hash){
      return db.createUser(username, hash)
    })
  },

  login: function(username, password){
    return db.userFind(username)
    .then(function(userObj){
      if(!userObj){
        console.log("did not find " + username + " in database.");
        return new Promise(function(resolve, reject){ 
          resolve({login:false, message:"Your username and/or password are incorrect."})
        }
      }
      else {
        console.log("found user: " + userObj._id, userObj);
        return new Promise(function(resolve, reject){
          bcrypt.compare(password, userObj.hashword, function(err, bool) {
            resolve({bool:bool, 
              user:userObj._id,
              mindSeal: userObj
            })
          })
        })
      } 
    })
  }
}

答案 1 :(得分:0)

 if ([[SDWebImageManager sharedManager] diskImageExistsForURL:[NSURL URLWithString:url]]) {

        [self.imageView setImage:[[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:url]];

    } else {
        [self.imageViewsd_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:url]
                                                        placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                                                                    options:SDWebImageRefreshCached progress:nil
                                                                  completed:nil];
    }

答案 2 :(得分:0)

试试这个 将下面的代码放在自定义单元格方法

- (void)awakeFromNib
{

self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;

self.layer.shadowColor = [[UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0] CGColor];
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.layer.shadowRadius = 3.0f;
self.layer.shadowOpacity = 1.0f;
self.layer.masksToBounds = NO;
CGRect shadowFrame = cell.layer.bounds;
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
self.layer.shadowPath = shadowPath;

self.backgroundColor = [UIColor whiteColor];
}

并从-cellForItemAtIndexPath

中删除它

试试这个