I Created UITableView, the data in that TableView loads from web service .And I insert a check box in the cell.
I would like to save the data of checked cell.
And also when i reopen the application the selected check boxes are remaining as it is like previous.
This is my myTable.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cell1=@"aa";
CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:cell1];
if (cell==nil) {
cell=[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell1];
NSArray *cellArray=[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:nil options:nil];
for (UIView *view in cellArray) {
if([view isKindOfClass:[CustomCell class]])
{
cell = (CustomCell*)view;
}
}
}
//cell.textLabel.text=[array objectAtIndex:indexPath.row];
cell.labelcell.text=[array objectAtIndex:indexPath.row];
return cell;
}
CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell{
BOOL isSelected;
}
@property (strong, nonatomic) IBOutlet UILabel *labelcell;
@property (strong, nonatomic) IBOutlet UIButton *checkButton;
- (IBAction)checkboxAction:(id)sender;
@end
CustomCell.m
- (IBAction)checkboxAction:(id)sender {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
if(!isSelected){
[checkButton setImage:[UIImage imageNamed:@"checked.png"]
forState:UIControlStateNormal];
isSelected = YES;
[defaults setBool:isSelected forKey:@"boxIsChecked"];
}
else if (isSelected){
[checkButton setImage:[UIImage imageNamed:@"empty-check.png"]
forState:UIControlStateNormal];
isSelected = NO;
[defaults setBool:isSelected forKey:@"boxIsChecked"];
}
[defaults synchronize];
}
-(void)checkTheBox{
if(!isSelected){
[checkButton setImage:[UIImage imageNamed:@"empty-check.png"]
forState:UIControlStateNormal];
}
else if (isSelected){
[checkButton setImage:[UIImage imageNamed:@"checked.png"]
forState:UIControlStateNormal];
}
}
Am try to use NSUserDefaults but it is applying for total rows in the table. I would like to apply for individual row and also want to save that row data. Can any one Help me.
Aucun commentaire:
Enregistrer un commentaire