In ios table view reuse the cell for every row so it does happen.
You use model class for manage selection . I have add selectors on button on cell .And also example of reload table view. Please check below code.
//
// ViewController.m
#import "ViewController.h"
#import "DataModel.h"
#import "MyTableViewCell.h"
@interface ViewController ()
{
NSMutableArray *arrData;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
arrData = [[NSMutableArray alloc]init];
// create 15 model and store in array
for (int i =0; i<15; i++)
{
DataModel *model = [DataModel new];
model.strSelected = @"";
[arrData addObject:model];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = [self.tblView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// tag add on buttons
cell.btnPPressed.tag = indexPath.row;
cell.btnFPressed.tag = indexPath.row;
cell.btnWPressed.tag = indexPath.row;
cell.btnNAPressed.tag = indexPath.row;
//method add on buttons
[cell.btnPPressed addTarget:self action:@selector(btnP:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnFPressed addTarget:self action:@selector(btnF:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnWPressed addTarget:self action:@selector(btnW:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnNAPressed addTarget:self action:@selector(btnNA:) forControlEvents:UIControlEventTouchUpInside];
//background color change on button
DataModel *model = [arrData objectAtIndex:indexPath.row];
if([model.strSelected isEqualToString:@"P"])
{
cell.btnPPressed.backgroundColor = [UIColor redColor];
}
else if([model.strSelected isEqualToString:@"F"])
{
cell.btnFPressed.backgroundColor = [UIColor redColor];
}
else if([model.strSelected isEqualToString:@"W"])
{
cell.btnWPressed.backgroundColor = [UIColor redColor];
}
else if([model.strSelected isEqualToString:@"NA"])
{
cell.btnNAPressed.backgroundColor = [UIColor redColor];
}
else
{
cell.btnPPressed.backgroundColor = [UIColor darkGrayColor];
cell.btnFPressed.backgroundColor = [UIColor darkGrayColor];
cell.btnWPressed.backgroundColor = [UIColor darkGrayColor];
cell.btnNAPressed.backgroundColor = [UIColor darkGrayColor];
}
return cell;
}
//buttons methods
-(void)btnP:(UIButton *)sender
{
sender.backgroundColor = [UIColor redColor];
DataModel *model = [arrData objectAtIndex:sender.tag];
model.strSelected = @"P";
//example reload table
dispatch_async(dispatch_get_main_queue(), ^{
[self.tblView reloadData];
});
}
-(void)btnF:(UIButton *)sender
{
sender.backgroundColor = [UIColor redColor];
DataModel *model = [arrData objectAtIndex:sender.tag];
model.strSelected = @"F";
//example reload table
dispatch_async(dispatch_get_main_queue(), ^{
[self.tblView reloadData];
});
}
-(void)btnW:(UIButton *)sender
{
sender.backgroundColor = [UIColor redColor];
DataModel *model = [arrData objectAtIndex:sender.tag];
model.strSelected = @"W";
//example reload table
dispatch_async(dispatch_get_main_queue(), ^{
[self.tblView reloadData];
});
}
-(void)btnNA:(UIButton *)sender
{
sender.backgroundColor = [UIColor redColor];
DataModel *model = [arrData objectAtIndex:sender.tag];
model.strSelected = @"NA";
//example reload table
dispatch_async(dispatch_get_main_queue(), ^{
[self.tblView reloadData];
});
}
@end
// DataModel.h
#import <Foundation/Foundation.h>
@interface DataModel : NSObject
@property(nonatomic, strong)NSString *strSelected;
@end
// MyTableViewCell.h
#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *btnPPressed;
@property (strong, nonatomic) IBOutlet UIButton *btnFPressed;
@property (strong, nonatomic) IBOutlet UIButton *btnWPressed;
@property (strong, nonatomic) IBOutlet UIButton *btnNAPressed;
@end
0
solved Why Selected value get deselect when I reload table view in objective c