当image为nil时,UICollectionViewCell不设置标签帧

时间:2014-07-29 11:37:34

标签: ios objective-c uicollectionview

我想要做的是,当没有UILabel时,我正在向UITextView帧和image向上移动。 当我在本地做这件事时,它工作正常。但是现在当图像从服务器下载时,当我检查nil图像时,它给了我错误的输出。 我正在尝试水平滚动 这是我试过的代码

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *myCell=nil;
myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"
                                forIndexPath:indexPath];


UIImageView *ImageView=(UIImageView*)[myCell viewWithTag:9];
UILabel *questionLabel=(UILabel*)[myCell viewWithTag:567];
UITextView *answerTextView=(UITextView*)[myCell viewWithTag:6];

questionLabel.text=[_questionsArray objectAtIndex:indexPath.row];

answerTextView.text=[_answerArray objectAtIndex:indexPath.row];
[ImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:/%@",[[_arrayOfDictionary objectAtIndex:indexPath.row] objectForKey:@"image"]]] ];

if (ImageView.image==nil) {
    //Always Satisfying this condition
    //Set Frame 

    return myCell;
}else{

    return myCell;
    }
}

请向我提供解决方案。以及为什么每次都满足if声明的原因。 (未选中Autolayout

1 个答案:

答案 0 :(得分:0)

添加AFNetworking以实现图像视图更新的异步解决方案,如

if (userBasicInfo.userImage == nil) {
            __weak LGMessageBoxCell *weakCell = cell;
            [cell.userImage setImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:userBasicInfo.imageUrl]]
                                  placeholderImage:[UIImage imageNamed:@"facebook-no-user.png"]
                                           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                                               weakCell.userImage.image = image;
                                               [weakCell setNeedsLayout];
                                               // reload your tableView current cell here
                                           }
                                           failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                           }];
        }