Custom TableView-Cell (Data from server) select multiple values with accessory change
customization, objective-c, uitableview
Solution
Your problem is:
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
Because it downloads all of the image data from the network each time you reload the table view. You should be doing this asynchronously and caching the returned image so you don't need to download it repeatedly. Take a look at a library like SDWebImage to help you with this.
Problem
I am working with TableView and wanted Custom TableViewCell to have a Small Image, Name and one custom image (With Tickmark and without Tick) can be on accessory to show if Cell is selected and if it's not selected it will show without Tick image on unselected cells. And if i want to select multiple cells then it should show Tick image on selected Cells and Untick image on unselected cells and after that when i click on a button then i should be able to get the selected cell id's. On the tableView i am getting all the values from the server and images also from URL's but the Tickmark and Unselected Tick mark image will be used the project itself. So far i have created : Class .h,.m,.xib of "ResultTableCell" of type UITableViewCell and my Main view "Result" with the TableView and a Button on top (on click of button i'll get the values of selected cells) ResultTableCell.h ``` #import <UIKit/UIKit.h> @interface ResultTableCell : UITableViewCell @property (nonatomic, retain) IBOutlet UILabel *nameLabel; @property (nonatomic, retain) IBOutlet UIImageView *thumbImageView; ``` ResultTableCell.m ``` #import "ResultTableCell.h" @implementation ResultTableCell @synthesize nameLabel,thumbImageView; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } ``` ResultTableCell.xib The right hand side image on the xib is the place where the accessor image will come. ResultTableCell.xib And the main xib Result.h ``` #import <UIKit/UIKit.h> #import "ASIFormDataRequest.h" @interface Results : UIViewController <UITableViewDelegate,UITableViewDataSource> { NSMutableArray *nameData; } @property (nonatomic, retain)NSMutableArray *nameData; @property (nonatomic, retain)NSMutableArray *ImageData; @property (nonatomic, retain)NSMutableArray *idData; @property (nonatomic, retain)UITableView *table; @property (nonatomic, retain) IBOutlet UIButton *done; @property (nonatomic, retain) NSMutableArray *arFors; -(IBAction)save_done:(id)sender; ``` Result.m ``` #import "Results.h" #import "ResultTableCell.h" @interface Results () @end @implementation Results @synthesize arFors; @synthesize done,nameData,table,addressData,ImageData,idData; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.arFors=[NSMutableArray array]; // I am Getting Name,id and image url data from my HomeViewController NSLog(@"Name Data from home view is %@",nameData); // 10 Names get's printed in log NSLog(@"id Data is %@",idData); NSLog(@"URL image data is %@",ImageData); table = [[UITableView alloc]initWithFrame:CGRectMake(0, 221, 320, 327) style:UITableViewStylePlain]; table.delegate = self; table.dataSource = self; [self.view addSubview:table]; -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSLog(@"Name data count is %d",nameData.count); return nameData.count; //return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 70; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /* UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MainCell"]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"]; }*/ static NSString *simpleTableIdentifier = @"ResultTableCell"; ResultTableCell *cell = (ResultTableCell *)[table dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ResultTableCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } if ([self.arFors containsObject:[NSNumber numberWithInt:indexPath.row]]) { cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"table_tick" ]]; } else { cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"table_add" ]]; } NSLog(@"data is ************* %@",nameData); cell.nameLabel.text = [nameData objectAtIndex:indexPath.row]; NSURL * imageURL = [NSURL URLWithString:[ImageData objectAtIndex:indexPath.row]]; NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage * image2 = [UIImage imageWithData:imageData]; cell.ImageView.image = image2; cell.ImageView.contentMode = UIViewContentModeScaleAspectFit; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"didSelectRowAtIndexPath %d",indexPath.row); if ([self.arFors containsObject:[NSNumber numberWithInt:indexPath.row]]) { [self.arFors removeObject:[NSNumber numberWithInt:indexPath.row]]; } else{ [self.arFors addObject:[NSNumber numberWithInt:indexPath.row]]; // [self.table cellForRowAtIndexPath:indexPath]; } [tableView deselectRowAtIndexPath:indexPath animated:YES]; [tableView reloadData]; } -(IBAction)save_done:(id)sender { NSLog(@"selected cell values are %@",self.arFors); } ``` Now with this code everything is working fine (tick image is shown on selected cells and untick image on unslected cells and on clicking the Done button i am getting the selected cell values), But the Problem comes when i tapp on a cell then it like hangs and takes 5-6 seconds of time to change accessor image as it fires [tableView reloadData] in didselectrowatindexpath method so all data reloads again in the tableview and then the accessor image changes, please can any one correct my code or enhance it so that it works fast. I have tried a lot of ways but i was not able to do it without the reloading of table and if i reload table it takes long time. Coding help will be much tankful.