cellForItemAtIndexPath is Changing Multiple Cells When Should Change One - ios

I may be not understanding the reusing of cells behaviour by the UICollectionView but can someone tell me why two cells are having their properties changed in cellForItemAtIndexPath when it should just be the one cell.
Here's my code.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoCollectionViewCell *cell = (PhotoCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:#"PhotoCollectionViewCell" forIndexPath:indexPath];
if (![[self.userPhotos objectAtIndex:indexPath.row] isEqualToString:#""]){
NSString *photoUrl = [self.userPhotos objectAtIndex:indexPath.row];
NSURL *imageURL = [NSURL URLWithString: photoUrl];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageURL];
[cell.photoImageView setImageWithURLRequest:imageRequest placeholderImage:[UIImage imageNamed:#"anImage"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
cell.photoImageView.image = image;
cell.photoImageView.frame = CGRectMake(0, 0, 100.0f,100.0f);
cell.photoImageView.contentMode = UIViewContentModeScaleAspectFit;
[cell.deleteImageView setFrame:CGRectMake(-4.0f,-4.0f,28.0f,28.0f)];
cell.deleteImageView.layer.cornerRadius = cell.deleteImageView.frame.size.width / 2;
[cell.deleteImageView setAlpha:0.8f];
cell.deleteImageView.layer.borderWidth = 1.0f;
cell.deleteImageView.layer.borderColor = [UIColor redColor].CGColor;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"%#",[error localizedDescription]);
}];
}else{
cell.photoImageView.image = [UIImage imageNamed:#"camera"];
cell.photoImageView.layer.borderWidth = 1.0f;
cell.photoImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
NSLog(#"setting alpha & hiding delete");
cell.photoImageView.alpha = 0.4f;
[cell.deleteImageView setHidden:YES];
}
The code in the else part is being executed on cells 2 and 6 for example when it should just be 6. Although the logging says it's just executed on 6
Any idea where I'm going wrong?
Thanks.

Related

Images in cell are changing everytime when tableview is scrolled

In the below code whenever I am scrolling the tableview, images in each cell are changing, which shouldn't happen. Please help. Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UserDetails *userDetails = [arrUserDetails objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"CustomCell";
__weak TableViewCell *cell = (TableViewCell *)[_tableViewUsername dequeueReusableCellWithIdentifier:CellIdentifier];
cell.tag = indexPath.row;
cell.userName.text = userDetails.userName;
[self.operationQueue addOperationWithBlock: ^ {
NSURL *aURL = [NSURL URLWithString:userDetails.userImageURL];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:aURL options:nil error:&error];
UIImage *image = nil;
if (cell.tag == indexPath.row)
{
image = [UIImage imageWithData:data];
[[NSOperationQueue mainQueue] addOperationWithBlock: ^ {
cell.customImageView.image = image;
cell.customImageView.contentMode = UIViewContentModeScaleToFill;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}];
}
}];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UserDetails *userDetails = [arrUserDetails objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"CustomCell";
__weak TableViewCell *cell = (TableViewCell *)[_tableViewUsername dequeueReusableCellWithIdentifier:CellIdentifier];
cell.tag = indexPath.row;
cell.userName.text = userDetails.userName;
//Add Default placeholder
cell.customImageView.image = [UIImage imageNamed:#"Default.png"];
[self.operationQueue addOperationWithBlock: ^ {
NSURL *aURL = [NSURL URLWithString:userDetails.userImageURL];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:aURL options:nil error:&error];
UIImage *image = nil;
if (cell.tag == indexPath.row)
{
image = [UIImage imageWithData:data];
[[NSOperationQueue mainQueue] addOperationWithBlock: ^ {
cell.customImageView.image = image;
cell.customImageView.contentMode = UIViewContentModeScaleToFill;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}];
}
}];
Add Default placeholder for image before loading it from url,As cells are being reused it takes the previous image
You can use SDWebImage.framework to load image
[cell.customImageView sd_setImageWithURL:[NSURL URLWithString:userDetails.userImageURL] placeholderImage:nil options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
[cell.customImageView setImage:image];
}
}];
You can use Afnetworking class for this .
Simply import #import "UIImageView+AFNetworking.h"
And use this line:-
[cell.imgProfile setImageWithURL:imgurl placeholderImage:[UIImage imageNamed:#""]];
Imgurl is the image Url which you are getting from response

Async Image Loading Performance

I am loading images in UITableViewCell receiving from url using below code, everything is working fine but the image load very slow in UITableViewCell and if i think as a user, it is not acceptable.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [checkData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger i =indexPath.row;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIImageView propertyImage = (UIImageView *)[cell viewWithTag:10];
NSString *test = [[checkData objectAtIndex:i]valueForKey:#"image"];
CGRect frame=propertyImage.bounds;
AsyncImageView asyImage=[[AsyncImageView alloc] initWithFrame:frame];
[asyImage setImage:[UIImage imageNamed:#"loading.png"]];
self.urlForTesting=[NSURL URLWithString:test];
[asyImage loadImageFromURL:self.urlForTesting];
asyImage.imageURL =self.urlForTesting;
asyImage.contentMode = UIViewContentModeScaleToFill;
asyImage.layer.masksToBounds=YES;
[propertyImage addSubview:asyImage];
return cell;
}
Use AFNetworking Class & add additional two file to download from github "UIImageView+AFNetworking" and import in your project. then after add this code from "cellForRowAtIndexPath"
// Fetch Image from URL
NSURL *url = [NSURL URLWithString:[[checkData objectAtIndex:i]valueForKey:#"image"]];
[objWinner.actWinner startAnimating];
[objWinner.actWinner setHidden:NO];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[objWinner.imgwinner setImageWithURLRequest:req placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
[objWinner.actWinner stopAnimating]; // this is Activityindicator
[objWinner.actWinner setHidden:YES];
objWinner.imgwinner.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
}];

Lazy loading in uicollectionview

here is the code for my collectionview it is showing records but loading really please tell me how can i implement lazy loading on this i also have a placeholder pic in my project
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MJCell" forIndexPath:indexPath];
// Setup image name
NSString *url = [[rssOutputData objectAtIndex:indexPath.row]xmllink];
UIImage *img = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
img = [[UIImage alloc] initWithData:data];
cell.MJImageView.image = img;
return cell;
}
right now it is working but very very slow.
It's pretty easy to do lazy loading using GCD.
// Create a queue for the operations
dispatch_queue_t queue = dispatch_queue_create("photoList", NULL);
// Start getting the data in the background
dispatch_async(queue, ^{
NSData* photoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:object.photoURL]];
UIImage* image = [UIImage imageWithData:photoData];
// Once we get the data, update the UI on the main thread
dispatch_sync(dispatch_get_main_queue(), ^{
cell.photoImageView.image = image;
});
});
The easiest way to implement that is use SDWebImage library, it does right what you need. There is UIImageView category that will allow you to modify code for that:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MJCell" forIndexPath:indexPath];
// Setup image name
NSString *url = [[rssOutputData objectAtIndex:indexPath.row]xmllink];
[cell.MJImageView sd_setImageWithURL:[NSURL URLWithString:url]];
return cell;
}
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: #"url.com"]];
UIActivityIndicatorView * indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[yourImageView addSubview:indicator];
indicator.center = yourImageView.center;
[indicator startAnimating];
[yourImageView setImageWithURLRequest:request
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
dispatch_async(dispatch_get_main_queue(), ^(void){
yourImageView.image = image;
});
[indicator stopAnimating];
[indicator removeFromSuperview];
} failure:nil];
Maybe, because your image is so large
You can use NSThread for loading
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MJCell" forIndexPath:indexPath];
cell.tag = indexPath.row; //For index
[NSThread detachNewThreadSelector:#selector(loadImage:) toTarget:self withObject:cell];
return cell;
}
- (void) loadImage:(CollectionViewCell *)cell {
NSString *url = [[rssOutputData objectAtIndex:cell.tag]xmllink];
UIImage *img = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
img = [[UIImage alloc] initWithData:data];
cell.MJImageView.image = img;
}

UICollectionView freezes iOS app

Good morning,
I'm using UICollectionView for the first time to show images from a user (like a Facebook profile) and at the moment I can show the images fine but I have some problems:
1- When I visit my profile the app freezes for like 2-3 minutes due to the load of 5 images.
2- When I'm moving through the UICollectionView it freezes when the app load again the images outside the screen.
What I have to do in order to not to freeze the app when loading the user pictures? And what I have to do to navigate through the CollectionView without freezing? Maybe a cache system is what I need?
That's my code:
ProfileViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor: [self colorWithHexString:#"FFFFFF"]];
self.profileimage.layer.cornerRadius = self.profileimage.frame.size.width / 2;
self.profileimage.clipsToBounds = YES;
self.profileimage.layer.borderWidth = 1.0f;
self.profileimage.layer.borderColor = [UIColor whiteColor].CGColor;
[self fetchJson];
[self fetchImages];
self.oneCollectionView.dataSource = self;
self.oneCollectionView.delegate = self;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 1;
}
-(NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _carImages.count;
}
// COLLECTION VIEW
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"MyCell"
forIndexPath:indexPath];
NSString *data = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"imagen"];
NSURL * imageURL = [NSURL URLWithString:data];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * images = [UIImage imageWithData:imageData];
myCell.imageview.image = images;
return myCell;
}
-(void)fetchImages {
self.carImages = [[NSMutableArray alloc] init];
NSString *usersPassword = [SSKeychain passwordForService:#"login" account:#"account"];
NSString * urlString = [NSString stringWithFormat:#"http://mywebsite.com/posts.php?usersPassword=%#",usersPassword];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError *error;
[_jsonArray removeAllObjects];
_jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
NSString* imagen = [jsonObject objectForKey:#"imagen"];
[_carImages addObject:imagen];
}
}
Thanks in advance.
Import UIImageView+AFNetworking.h
and load your image via this method in cellForItemAtIndexPath method
[imageView setImageWithURL:[NSURL URLWithString:#"https://lh6.googleusercontent.com/-B8kSXtoaQDo/VGTVlXyIXpI/AAAAAAAAJ_M/USh6SgvMemw/w1024-h1024/IMG_20141112_103152.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder-avatar"]];
it will surely speed up to load and scrolling collectionView
Download the images asynchronously, dataWithContentsOfURL is synchronous method and it will block your current thread until the download completes. You can use libraries like SDWebImage to automatically handle downloading for you or You can use NSURLSessionDownloadTask to download Images.
- (void)fetchImages {
self.carImages = [[NSMutableArray alloc] init];
NSString *usersPassword = [SSKeychain passwordForService:#"login" account:#"account"];
NSString * urlString = [NSString stringWithFormat:#"http://mywebsite.com/posts.php?usersPassword=%#",usersPassword];
NSURL * url = [NSURL URLWithString:urlString];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
[self.jsonArray removeAllObjects];
self.jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = self.jsonArray[i];
NSString* imagen = jsonObject[#"imagen"];
[self.carImages addObject:imagen];
}
}
}];
[dataTask resume];
}
// COLLECTION VIEW
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"MyCell"
forIndexPath:indexPath];
NSString *data = [[self.jsonArray objectAtIndex:indexPath.row] valueForKey:#"imagen"];
NSURL * imageURL = [NSURL URLWithString:data];
NSURLSessionDownloadTask *imageDownloadTask = [[NSURLSession sharedSession]
downloadTaskWithURL:imageURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
UIImage *image = [UIImage imageWithData:
[NSData dataWithContentsOfURL:location]];
myCell.imageview.image = image;
}];
[imageDownloadTask resume];
return myCell;
}
You can use the dispatcher to create an async operation for the download of the images. This will resolve the 2 problems you have:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:YOUR_IMAGE_URL];
UIImage *img = [UIImage imageWithData:imgData];
[YOUR_IMAGE_VIEW_OUTLET performSelectorOnMainThread:#selector(setImage:) withObject:img waitUntilDone:YES];
});
These are the snippet you have to change:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"MyCell"
forIndexPath:indexPath];
NSString *data = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"imagen"];
NSURL * imageURL = [NSURL URLWithString:data];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL: imageURL];
UIImage *img = [UIImage imageWithData:imageData];
[myCell.imageview performSelectorOnMainThread:#selector(setImage:) withObject:img waitUntilDone:YES];
});
return myCell;
}
Try to Register Nib For Collection View
Write following code in your viewController's viewDidLoad()method :
UINib *nib = [UINib nibWithNibName:#"MyCollectionCell" bundle: nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:#"Cell"];
And I think you have to use https://github.com/nicklockwood/AsyncImageView for the image loading in collection view.
For Storyboards you have to see this tutorial : http://www.appcoda.com/ios-programming-uicollectionview-tutorial/ This will help you more.
Thanks!
For the first question the answer is in this line of code:
NSData * data = [NSData dataWithContentsOfURL:url];
From Apple Reference:
Do not use this synchronous method to request network-based URLs. For
network-based URLs, this method can block the current thread for tens
of seconds on a slow network, resulting in a poor user experience, and
in iOS, may cause your app to be terminated.
As alternative you can use NSURLSessionDataTask to download data (see Apple Reference)
-Edit
In ProfileViewController.h add these two properties:
#property (nonatomic, strong) NSURLSessionConfiguration *sessionConfig;
#property (nonatomic, strong) NSURLSession *session;
then, in - viewDidLoad initialise them:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view
self.sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:self.sessionConfig];
//Other stuff...
}
Finally, in ProfileViewController.m
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"MyCell"
forIndexPath:indexPath];
NSString *data = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"imagen"];
NSURL * imageURL = [NSURL URLWithString:data];
NSURLSessionDownloadTask *imageDownloadTask = [self.session dataTaskWithURL:imageURL
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(#"ERROR: %#", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200) {
UIImage *image = [UIImage imageWithData:data];
myCell.imageview.alpha = 0.0f;
myCell.imageview.image = image;
[UIView animateWithDuration:0.45 animations:^{
myCell.imageview.alpha = 1.0f;
});
} else {
NSLog(#"Couldn't load image at URL: %#", imageURL);
NSLog(#"HTTP %d", (int)httpResponse.statusCode);
}
}
}];
[imageDownloadTask resume];
return myCell;
}
I hope this can help you.
- Edit 2
For future readers, I slightly refactored my code based on #suhit's answer (+1 for him)

why the table view cells are messed up after refreshing some rows?

I am using UITableView to list some images from Internet. So i use AFNetworking's setImageWithURLRequest:placeholderImage:success:failure:methord, when the image is downloaded, i need to do some process then display. In order to refresh the processed image i use
[wTableView beginUpdates];
[wTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[wTableView endUpdates];
in the success block. It seems this works when first time loaded, but when i scroll the tableview, the rows are messed up, also one row is disappeared and its easy to crash in the [wTableView endUpdates]; method. what's wrong with this method?
The related code snippet is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = #"cell";
CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (!cell) {
cell = [[CouponTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableIdentifier];
}
[cell prepareForReuse];
NSArray *coupons = [mCoupons objectAtIndex:indexPath.section];
CouponDB *couponDB = [coupons objectAtIndex:indexPath.row];
NSString *thumbLink;
if (couponDB) {
[cell.textLabel setText:couponDB.couponInfo.company];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:CELL_LABEL_FONT_SIZE]];
[cell.textLabel setTextColor:[UIColor grayColor]];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.detailTextLabel setText:[NSString stringWithFormat:#"Expires:%#",couponDB.couponInfo.deadline]];
[cell.detailTextLabel setBackgroundColor:[UIColor clearColor]];
thumbLink = [self thumbLink:couponDB.couponInfo.imgURL];
[cell setNeedsLayout];
if (couponDB.redeem_status) {
UIImageView * __weak imageView = cell.imageView;
CouponTableController * __weak table = self;
UITableView *__weak wTableView = mTableView;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thumbLink]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
imageView.image = [table crossedImage:image];
#synchronized(wTableView){
[wTableView beginUpdates];
[wTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[wTableView endUpdates];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"cell imageview load error:%#",error);
}];
} else {
[cell.imageView setImageWithURL:[NSURL URLWithString:thumbLink] placeholderImage:[UIImage imageNamed:#"default_cover.jpg"]];
}
}
if (![cell.backgroundView isKindOfClass:[CouponTableCellBackgroud class]]) {
cell.backgroundView = [[CouponTableCellBackgroud alloc] init];
}
return cell;
}
the first time the table is loaded(no scroll) is like this:
when you scroll down and up the table rows are messed up, which is like below:
Any suggestions are appreciated.
Try this, instead of reloading the cell. Declare a weak pointer to the cell
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thumbLink]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.imageView.image = [table crossedImage:image];
[weakCell setNeedsLayout];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"cell imageview load error:%#",error);
}];
Can you try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"YourResuseIdentifier";
CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier];
}
else
{
for (UIView *subview in cell.contentView.subviews)
[subview removeFromSuperview];
}
NSArray *coupons = [mCoupons objectAtIndex:indexPath.section];
CouponDB *couponDB = [coupons objectAtIndex:indexPath.row];
NSString *thumbLink;
if (couponDB) {
[cell.textLabel setText:couponDB.couponInfo.company];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:CELL_LABEL_FONT_SIZE]];
[cell.textLabel setTextColor:[UIColor grayColor]];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.detailTextLabel setText:[NSString stringWithFormat:#"Expires:%#",couponDB.couponInfo.deadline]];
[cell.detailTextLabel setBackgroundColor:[UIColor clearColor]];
thumbLink = [self thumbLink:couponDB.couponInfo.imgURL];
if (couponDB.redeem_status) {
UIImageView * __weak imageView = cell.imageView;
CouponTableController * __weak table = self;
UITableView *__weak wTableView = mTableView;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thumbLink]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
imageView.image = [table crossedImage:image];
[wTableView beginUpdates];
[wTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[wTableView endUpdates];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"cell imageview load error:%#",error);
}];
} else {
[cell.imageView setImageWithURL:[NSURL URLWithString:thumbLink] placeholderImage:[UIImage imageNamed:#"default_cover.jpg"]];
}
}
if (![cell.backgroundView isKindOfClass:[CouponTableCellBackgroud class]]) {
cell.backgroundView = [[CouponTableCellBackgroud alloc] init];
}
return cell;
}
I change the load image code to
UIImageView * __weak imageView = cell.imageView;
CouponTableController * __weak table = self;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thumbLink]] placeholderImage:[UIImage imageNamed:#"default_cover.jpg"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
imageView.image = [table crossedImage:image];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"cell imageview load error:%#",error);
}];
Only add one placeholder image and remove the tableview updates. then it refresh cells automatically.

Resources