我有一个表格视图,我把一个按钮放在一个tableview上作为一个复选框,现在我想保持复选框的状态。我的问题是当我从一个视图控制器导航到另一个或终止我的应用程序时所有复选框都是未选中的以前检查过。任何建议都值得赞赏。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str=[NSString stringWithFormat:@"identfier%d",indexPath.row];
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
UILabel *lblValue;
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
lblValue=[[UILabel alloc]init ];
lblValue.tag=indexPath.row+20000;
btnValue=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btnValue.tag=indexPath.row+100;
}else{
lblValue=(UILabel*)[cell.contentView viewWithTag:indexPath.row+20000];
btnValue=(UIButton*)[cell.contentView viewWithTag:indexPath.row+100];
}
lblValue.backgroundColor=[UIColor clearColor];
lblValue.text=[[sortedArray objectAtIndex:indexPath.row]valueForKey:@"CategoryName"];
//lblValue.text=@"some value";
UIImage *buttonImage = [UIImage imageNamed:@"radiou.png"];
UIImage *buttonImage1=[UIImage imageNamed:@"radioc.png"];
[btnValue setBackgroundImage:buttonImage forState:UIControlStateNormal];
[btnValue setBackgroundImage:buttonImage1 forState:UIControlStateSelected];
[btnValue addTarget:self action:@selector(checkedButton:) forControlEvents:UIControlEventTouchUpInside];
btnValue.frame=CGRectMake(10, 15, 25, 25);
if([self.navigationItem.rightBarButtonItem.title isEqual:@"Done"]){
[UITableView animateWithDuration:0.3f delay:0.1f options:UIViewAnimationOptionCurveEaseInOut animations:^{
lblValue.frame=CGRectMake(35+25, 17, 200, 20);
btnValue.hidden=NO;
} completion:nil];
}
else
{
[UITableView animateWithDuration:0.3f delay:0.1f options:UIViewAnimationOptionCurveEaseInOut animations:^{
lblValue.frame=CGRectMake(35, 17, 200, 20);
btnValue.hidden=YES;
} completion:nil];
}
[cell.contentView addSubview:btnValue];
[cell.contentView addSubview:lblValue];
return cell;
}
答案 0 :(得分:2)
为此,您可以创建一个包含复选框值的NSArray。现在,当调用viewWillDissappear时,您可以将此数组保存在NSUserDefaults和viewWillAppear中,您可以从NSUserDefaults中检索数组并相应地设置复选框中的值。
假设你的表中有10行,那么你可以创建一个数为10的数组。现在如果选中第4行和第5行的复选框,那么你可以将1放在索引3和4,其余的索引应该是0
int arrayCount = 10;//enter number of rows you have
NSMutableArray *checkStatusArray = [[NSMutableArray alloc]init];
for (int i=0; i<arrayCount; i++)
{
if([[checkBoxStatusArray objectAtIndex: i]integerValue] == 1)
// Above mentioned array should be made prior to this method from which checkbox will be loaded or you can make 0 entries here for every indexInitially
{
[checkStatusArray addObject:[NSString stringWithFormat:@"1"]];
}
else
{
[checkStatusArray addObject:[NSString stringWithFormat:@"0"]];
}
}
// To put value
[[NSUserDefaults standardUserDefaults]setObject:checkStatusArray forKey:@"statusArray"];
// Now to fetch value
NSArray *valuesArray = [[NSUserDefaults standardUserDefaults]objectForKey: @"statusArray"];
答案 1 :(得分:1)
如果要在viewControllers中保持状态而不保存状态并将状态加载到持久存储,则应该使其viewControllers属性为其parentViewControllers。
这样,当你离开它们时,它们不会被释放。
你还必须记住只分配和初始化它们一次。