如何在iOS中切换控制器时保留TableViewCell颜色

如何在iOS中切换控制器时保留TableViewCell颜色,ios,uitableview,uikit-state-preservation,Ios,Uitableview,Uikit State Preservation,我已经创建了包含第一类viewcontroller和第二类CheckListTableController的故事板segue应用程序。类别(按钮)方面,我正在CheckListTableController的tableview上打开不同的问题列表 我已经在tableView单元格中设置了“选中”和“取消选中”按钮及其各自的颜色。我使用了NSMutableArray,并在其中添加了indexPath来设置单元格的特定颜色 当我在这些控制器之间导航时,我希望我的tableView单元格颜色应保留 你

我已经创建了包含第一类viewcontroller和第二类CheckListTableController的故事板segue应用程序。类别(按钮)方面,我正在CheckListTableController的tableview上打开不同的问题列表

我已经在tableView单元格中设置了“选中”和“取消选中”按钮及其各自的颜色。我使用了NSMutableArray,并在其中添加了indexPath来设置单元格的特定颜色

当我在这些控制器之间导航时,我希望我的tableView单元格颜色应保留

你知道怎么做吗。提前谢谢

使用查询13编辑

现在我需要使用plist文件或核心数据来实现相同的功能。这样用户就可以保存任务的会话并在以后访问它。在第一节课中,他确实勾选了ABC大楼的取消勾选标记,然后保存了该节课。第二课时,他再次检查/取消勾选不同位置XYZ建筑的活动,然后保存

哪种方法适合这里。Plist文件还是核心数据?你能把我引向具体的解决方案吗

使用一些代码进行编辑

Catergory_ViewController.m 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{
if([segue.identifier isEqualToString:@"chkListTable"])
{
     NSString *bldgArrayString = [[NSString alloc] initWithFormat:@"bldg"];
    checkListTableViewController *chk= segue.destinationViewController;

    chk.gotquestArrayString = bldgArrayString;   

}
}



checkListTableViewController.m 


- (void)viewDidLoad
{
[super viewDidLoad];
if ([gotquestArrayString isEqual:@"bldg"])
{

    buildingQuest = [[NSMutableArray alloc]initWithObjects:@"Motor vehicles are not  parked in the space",

                     @"Lightning protection system (Electronic)",

                     @"Lightning protection systems available on all buildings",

                     @"Reception facilities in order and not damaged",
}
}
 //tableView button method where indexPath added to MutableArray
-(void)yesAction:(id)sender
{
arrayCheckUnchek = [[NSMutableArray alloc]init];
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
UITableView *curTableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [curTableView indexPathForCell:cell];
cell.backgroundColor = [UIColor greenColor];
[arrayCheckUnchek addObject:indexPath];
}

- (void)tableView: (UITableView*)tableView
 willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
// UITableViewCell *Cell = [myChklist cellForRowAtIndexPath:indexPath];
 if ([arrayCheckUnchek containsObject:indexPath])
 {
    cell.backgroundColor = [UIColor greenColor];
 }
 }

1.当您更改选择(选中/未选中)时,您需要将NSMutable数组保存在Plist文件或UserDefaluts中。此时,您应该更新Plist或UserDefaluts 2.在tableviewcontroller的ViewWillApper方法中读取plist文件。并将此数据传递给“ArrayCheck”

希望它对你有用

快乐偶像

  • 恩萨南特

加载表视图时,请放置条件并根据该数组更改单元格背景颜色。我已放置了一些代码。你能检查一下吗。当你这样做的时候发生了什么?你是说我应该在ViewDieload中写下这个条件,如果([arrayCheckUnchek containsObject:indexPath])。它并没有给欲望带来结果。不工作:(将此条件保留在CellForRowatinex mathod上,并在出现该视图时重新标记tableview。我明白您使用UserDefaults的意思。但是,如果只保存一个数组,我在ViewDidLoad中的代码会是什么?我如何检索单元格颜色?您应该创建一个字典数组,这样您就可以为单个单元格保存任意数量的数据/属性,例如:ans颜色、单元格数据、单元格是否被选中、单元格类型等,并在tableview的cellForRow数据源方法[array objectAtindex:indexpath.row];[mutableDict objectForKey:@“Color”];并且您不需要在ViewDidLoad方法中编写任何代码,我建议您在ViewWillApper方法中编写代码,只需重新加载表viewHi NSANT即可。我对该函数有了新的查询。您可以检查我的新更新问题,并在上面指定日期。提前感谢。