【问题标题】:how to retain TableViewCell colour while toggling controllers in iOS如何在 iOS 中切换控制器时保留 TableViewCell 颜色
【发布时间】:2013-05-27 13:22:06
【问题描述】:

我创建了包含第一个 CategoryViewController 和第二个 CheckListTableController 的 StoryBoard segue 应用程序。类别(按钮)明智我在 CheckListTableController 的 tableview 上打开不同的问题列表。

我在 tableView 单元格中设置了选中和取消选中按钮及其各自的颜色。我采用 NSMutableArray 并将 indexPath 添加到此以将特定颜色设置为单元格。

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

任何想法如何做到这一点。提前致谢。

使用查询 26Aug13 编辑

现在我需要使用 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];
 }
 }

【问题讨论】:

  • 当您加载表格视图放置条件并根据该数组更改单元格背景颜色时。
  • 我已经放了一些代码。请检查一下。
  • 当你这样尝试时发生了什么?
  • 你在说我应该在 ViewDieload if ([arrayCheckUnchek containsObject:indexPath]) 中写下这个条件。它没有给出欲望的结果。不工作:(
  • 在 cellForRowAtIndex mathod 上保持这个条件,当你来到那个视图时重新加载 tableview。

标签: ios uitableview uikit-state-preservation


【解决方案1】:

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

希望对你有用

快乐的编码......

  • NSAnant

【讨论】:

  • 我明白你使用 UserDefaults 的意思。但是只保存一个数组后,我在 ViewDidLoad 中的代码是什么,如何检索单元格颜色?
  • 你应该创建一个字典数组,这样你就可以为单个单元格保存任意数量的数据/属性,意味着颜色、单元格的数据、单元格是否被选中、单元格类型等,并使用它在 tableview [array objectAtindex:indexpath.row] 的 cellForRow Datasource 方法中; [mutableDict objectForKey:@"Color"];而且您不需要在 ViewDidLoad 方法中编写任何代码,我建议您在 ViewWillApper 方法中编写代码并简单地重新加载您的表格视图
  • 您好,NSAnant。我对此功能有新的查询,您可以检查我上面指定日期的新更新问题。提前致谢。
猜你喜欢
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
相关资源
最近更新 更多