【发布时间】:2014-02-10 08:17:37
【问题描述】:
我有一个表格视图,我将按钮放在表格视图上作为复选框,现在我想保持复选框的状态。我的问题是当我从一个视图控制器导航到另一个或终止我的应用程序时,所有复选框都未选中之前检查过。任何有意义的建议。
- (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;
}
【问题讨论】:
-
您应该在 cellForRowAtIndexPath 中恢复按钮选中状态,因为在重新加载表格按钮后,默认情况下会取消选中。因此,您需要一个数据结构来存储按钮状态。
-
@vokilam 抱歉,但我想在我的应用程序终止后主要状态
标签: ios iphone xcode cocoa-touch