samedi 14 mars 2015

Loading UIImage inside UItableViewCell not working as expected


Vote count:

0




I have read many questions and answers [here or on the web] through this day and I can't find an answer to my problem.


The situation is :


I have a custom UITableViewCell that contains a UIImageView, I load the content of the cell from a server and this is working as expected. for the image view I started a new thread to download the image, but the image is not showing UNTIL I download the whole items in my DataBase !


I have tried putting a link to a sample image in the internet and this works little bit okay [ with some delay ]


The problems : 1. The UIImgeView does not set its image as Until I load the whole table. 2. The UIActivityIndicator stops running before loading the image.


I think I am doing one thing wrong that causes those problems.


here is my code :



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

OfferTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"OfferCell"];
cellHeight = cell.frame.size.height;


Offer * tmpOffer = _offersArray[indexPath.row];

[cell.likeButtonOutlet addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
[cell.disLikeButtonOutlet addTarget:self action:@selector(disLike:) forControlEvents:UIControlEventTouchUpInside];

//Offer Title
cell.offerTitle.text = tmpOffer.OfferTitle;

//Offer Image
[cell.offerImage setContentMode:UIViewContentModeScaleAspectFit];
cell.offerImage.image = [UIImage imageNamed:@"placeholder.png"];
if(!tmpOffer.imageLoaded){

dispatch_async(dispatch_get_global_queue(0,0), ^{
UIActivityIndicatorView * indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[cell.offerImage addSubview:indicator];
[indicator startAnimating];
tmpOffer.OfferImageObject = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:tmpOffer.OfferImage]]];
tmpOffer.imageLoaded = true;
[indicator stopAnimating];
[indicator removeFromSuperview];
[cell.offerImage setImage:tmpOffer.OfferImageObject];
cell.offerImage.clipsToBounds =YES;
cell.offerImage.layer.cornerRadius = 5;
tmpOffer.imageLoaded = true;
});
}

//Refresh the table if it shows the last cell
if(indexPath.row == _offersArray.count-1){
UILabel * updating = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 70, 30)];
updating.text = @"جاري التحديت ...";
#warning show updating message

[self refresh];

}
return cell;
}


and here is the refreshing method :



-(void)refresh{
NSMutableString * path = [NSMutableString stringWithString: @"api/Offers?$top=4&$skip="];
NSUInteger numOfElements = [_offersArray count];
NSString * strNumOfElements =[NSString stringWithFormat:@"%ld",(long)numOfElements];
[path appendString:strNumOfElements];
[[RKObjectManager sharedManager]getObjectsAtPath:path parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)

{
int start = (int)_offersArray.count;



if(mappingResult.array.count !=0){
[_offersArray addObjectsFromArray: [mappingResult.array mutableCopy]];
NSMutableArray * indexesToBeReloaded = [[NSMutableArray alloc]init];
for(int i =start ; i<(int)_offersArray.count;i++){
[indexesToBeReloaded addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}

[self.tableView insertRowsAtIndexPaths:indexesToBeReloaded withRowAnimation:UITableViewRowAnimationNone];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"لا عروض جديدة الان " message: @"وصلت إلى ابق بالقرب لمتابعة أفضل العروض (:" delegate:nil cancelButtonTitle:nil otherButtonTitles: @"حسناً",nil];
[alert show];
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:error.description delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: @"",nil];
[alert show];
}

];


}


your help is really appreciated ..



asked 38 secs ago







Loading UIImage inside UItableViewCell not working as expected

Aucun commentaire:

Enregistrer un commentaire