Reputation: 275
I know here so many answers of this question but i unable to get proper meaning to all.
I want to button selection not cell. by default button is unchecked and if some one click or press button than button is changed to checked(only presses button) not a all.
I mentioned my screenshot and code there is button , array's values (appliances's name) and something with switch button
code
-(IBAction)SelectAppliance:(id)sender {
UIButton *btn=(UIButton*)sender;
NSLog(@"button tag is :%ld",(long)btn.tag);
NSLog(@"click button");
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_arrApplnc count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SceneTableViewCell";
SceneTableViewCell *cell = (SceneTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(checked) {
[cell.btnRadio setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
} else {
[cell.btnRadio setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
}
cell.btnRadio.tag = indexPath.row;
cell.lblName.text =[[_arrApplnc valueForKey:@"applianceName"] objectAtIndex:indexPath.row];
[cell.btnRadio addTarget:self action:@selector(SelectAppliance:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Upvotes: 0
Views: 917
Reputation: 115083
If you are going to use a button in the cell, I would suggest using a delegation pattern so that the cell itself handles the button tap and sends a message to the view controller.
In SceneTableViewCell.h
@protocol SceneTableViewCellDelegate <NSObject>
@optional
- (void)checkBoxTapped:(SceneTableViewCell *)cell;
@end
@interface SceneTableViewCell : NSObject
@property (nonatomic, weak) id <SceneTableViewCellDelegate> delegate;
@end
In SceneTableViewCell.m
-(IBAction)checkboxTapped:(UIButton *)sender {
[self.delegate checkBoxTapped:self];
}
In your ViewController.m
@property (nonatomic,strong) NSMutableIndexSet *checkedRows;
- (void) viewDidLoad {
self.checkedRows = [[NSMutableIndexSet alloc]init];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SceneTableViewCell";
SceneTableViewCell *cell = (SceneTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.delegate = self
}
if([self.checkedRows contains:indexPath.row]) {
[cell.btnRadio setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
} else {
[cell.btnRadio setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
}
cell.lblName.text =[[_arrApplnc valueForKey:@"applianceName"] objectAtIndex:indexPath.row];
return cell;
}
- (void) checkBoxTapped:(SceneTableView Cell *)cell {
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
if ([self.checkedRows contains: indexPath.row]) {
[self.checkedRows removeIndex: indexPath.row];
} else {
[self.checkedRows addIndex: indexPath.row];
}
[tableView reloadRowsAt:@[indexPath], withRowAnimation: UITableViewRowAnimationAutomatic];
}
Another approach would be to simply to use a UIImageView
in the cell rather than a UIButton
and use the didSelect
handler to toggle the checked state. That way the user can tap anywhere in the cell.
Upvotes: 1