2014-06-22 48 views
1

使用SDWebImage将gif加载到我的UITableViewCell中。它实际上非常快,但tableview似乎不会加载,直到用户实际滚动tableview使用UITableViewCell加载问题

有关如何解决此问题的任何建议?

这是我UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return self.gifArray.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *MyIdentifier = @"Cell"; 

RDGifGridTableViewCell *cell = (RDGifGridTableViewCell *)[tableView  dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RDGifGridTableViewCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

cell.urlLabel.text = [self.gifArray objectAtIndex:indexPath.row]; 
cell.urlLabel.textColor = [UIColor clearColor]; 

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:cell.urlLabel.text] placeholderImage:nil options:SDWebImageCacheMemoryOnly]; 

return cell; 
} 

这是我如何添加内容到发生在viewDidLoad中数组:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
      NSArray *idArray = [json objectForKey:@"data"]; 
       for (NSDictionary *ids in idArray) { 

        NSDictionary *images = ids[@"images"]; 
        NSDictionary *fixedHeightImage = images[@"fixed_width"]; 
        self.gifURL = fixedHeightImage[@"url"]; 
        [self.gifArray addObject:self.gifURL]; 
        [self.tableView reloadData]; 
       } 
+0

你可以发布“sd_setImageWithURL:”方法的代码。我认为它有问题... – Yogendra

回答

1

下面一行可能是问题

static NSString *MyIdentifier = @"Cell"; 

当你的细胞被重复使用时,你正在使用一个不同的细胞鉴定器RDGifGridTableViewCell

它应该是重复使用的单元格。

所以,只要修复这一行,并再次使用该变量时,它是零,以避免这样的错误,哦,当你在它的时候,考虑重命名变量为第一个字母小写字母myIdentifier目标C命名约定建议。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *MyIdentifier = @"RDGifGridTableViewCell"; 

RDGifGridTableViewCell *cell = (RDGifGridTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RDGifGridTableViewCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

cell.urlLabel.text = [self.gifArray objectAtIndex:indexPath.row]; 
cell.urlLabel.textColor = [UIColor clearColor]; 

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:cell.urlLabel.text] placeholderImage:nil options:SDWebImageCacheMemoryOnly]; 

return cell; 
} 
+0

这似乎并没有解决它,梅达。 – user3765569

+0

用'Cell'作为标识符来尝试,它只能是一个或另一个 – meda

+0

单元是这里的标识符.... – user3765569