Reputation: 4232
In my ios app I have added on one button in UITableViewCell
which is for check and un-check the TableViewCell products. For this I wrote below code but according to my code by default all are checked and when I scroll check boxes are un-checking
- (void)viewDidLoad {
[super viewDidLoad];
checkBoxesArray = [[NSMutableArray alloc]init];
for(int i = 0; i <15; i++){
[checkBoxesArray addObject:@""];
}
}
//TableList Delegate Methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return checkBoxesArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"HistoryCell";
UITableViewCell *cell = (UITableViewCell *)[MaintableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
model1 = mainArray[indexPath.row];
cell.textLabel.text = model1.Name;
cell.detailTextLabel.text = model1.MasterId;
bool variable = checkBoxesArray[indexPath.row];
newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[newBtn setFrame:CGRectMake(250,5,30,30)];
[newBtn addTarget:self action:@selector(urSelector:) forControlEvents:UIControlEventTouchUpInside];
UIImage *btnImage;
if(variable == YES){
NSLog(@"1");
btnImage = [UIImage imageNamed:@"check.png"];
}else{
NSLog(@"2");
btnImage = [UIImage imageNamed:@"uncheck.png"];
}
[newBtn setImage:btnImage forState:UIControlStateNormal];
[cell addSubview:newBtn];
return cell;
}
-(void)urSelector :(UIButton*)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:MaintableView];
NSIndexPath *indexPath1 = [MaintableView indexPathForRowAtPoint:buttonPosition];
NSInteger variable = indexPath1.row;
bool variablePosition = checkBoxesArray[variable];
if (variablePosition == YES){
variablePosition= NO;
[checkBoxesArray replaceObjectAtIndex:variable withObject:[NSString stringWithFormat:@"%d",variablePosition]];
}
else{
variablePosition = YES;
[checkBoxesArray replaceObjectAtIndex:variable withObject:[NSString stringWithFormat:@"%d",variablePosition]];
}
[MaintableView reloadData];
}
@end
Upvotes: 0
Views: 102
Reputation: 2640
1) Add one key
to your model array. Like isSelected
and set it's value to NO
.
2) Now when you select any cell at that time set the value of that key to YES
.
3) In cellForRow
, access that key which we have added and check it's value. If it's YES then set Check Image else Uncheck image.
4) Don't maintain two array so remove your checkboxarray
. It will create confusion for you.
Upvotes: 1
Reputation: 798
First Please replace your line of code in cellForRowAtIndexPath
bool variable = checkBoxesArray[indexPath.row];
with
bool variable = [checkBoxesArray[indexPath.row] length] > 0;
Because in checkBoxesArray , there are NSString type objects. and you are storing them into a bool variable . It always returns a true value to your 'variable'. So first of all you need to have a proper condition for 'check' and 'uncheck' thing. I am assuming that you want all boxes unchecked for the first time. So I put blank string for uncheck button and some value string for check button. Again you need to replace your code in urSelctor method
bool variablePosition = checkBoxesArray[variable];
if (variablePosition == YES){
variablePosition= NO;
[checkBoxesArray replaceObjectAtIndex:variable withObject:[NSString stringWithFormat:@"%d",variablePosition]];
}
with
bool variablePosition = [checkBoxesArray[variable] length] > 0;
if (variablePosition == YES){
variablePosition= NO;
[checkBoxesArray replaceObjectAtIndex:variable withObject:@""];
}
hope these things will help you.
Happy coding... :) :) :)
Upvotes: 0