I work with TableView and I want Custom TableViewCell to have a small image, the name and one user image (with a ticker and no tick) can be on the accessory to show if a cell is selected, and if it is not selected, it will be displayed without Tick image on unselected cells. And if I want to select several cells, then it should show the tick image on the selected cells and "Push the image" on the unselected cells, and after that, when I click the button, I should be able to get the selected cell identifiers.
In the View table, I get all the values from the server and the image also from the URL, but the Tickmark and Unselected Tick image will be used by the project itself.
So far I have created: Class.h, .m, .xib "ResultTableCell" of type UITableViewCell and my main view "Result" with TableView and the button at the top (when I click the button I get the values of the selected cells)
ResultTableCell.h
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) {
ResultTableCell.xib The image of the right side on xib is where the access image appears. ResultTableCell.xib
And the main xib Result.h
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 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 everything works fine with this code (the checkmark image is displayed on the selected cells and disables the image on the unselected cells, and when I click "Finish" I get the selected cell values),
But the problem arises when I click on the cell, then it looks like it hangs and takes 5-6 seconds to change the access image, because it calls [tableView reloadData] in the didselectrowatindexpath method, so all the data is reloaded again in the table view, and then The image of the accessory will change, please, anyone can fix my code or improve it so that it works quickly. I tried many ways, but I could not do this without reloading the table, and if I reload the table, it takes a lot of time. Help with coding will be very intense.