gpt4 book ai didi

ios - 无法保存复选标记以显示在 UITableView 上

转载 作者:行者123 更新时间:2023-11-28 21:48:02 24 4
gpt4 key购买 nike

我有一个 UITableView,用户可以在其中点击一个单元格,然后在单元格附件 View 上显示一个复选标记。当单元格被点击时,索引路径被保存到一个数组,该数组被保存到磁盘。

当我离开并返回 View Controller 时,我无法让复选标记重新出现在 UITableView 上。

- (void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"selectedCells"];
self.retrievedIndexPaths = [[NSArray alloc] initWithObjects:

[NSKeyedUnarchiver unarchiveObjectWithData:data], nil];

NSLog(@"%@", self.retrievedIndexPaths);

}

可能是我哪里做错了:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = self.categoriesArray[indexPath.row];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];

for (NSIndexPath *retrievedIndexPath in self.retrievedIndexPaths)
{
if (retrievedIndexPath == indexPath)
{ // Never hits here
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}

return cell;

}

保存复选标记数据:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedRowsArray addObject:cell.textLabel.text];
self.lastIndexPath = indexPath;
[self.selectedIndexPathsMutableArray addObject:self.lastIndexPath];

}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedRowsArray removeObject:cell.textLabel.text];
[self.selectedIndexPathsMutableArray removeObject:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];

NSLog(@"%@", self.selectedRowsArray);
NSLog(@"%@", self.selectedIndexPathsMutableArray);
}

#pragma mark - UIBUTTON

- (IBAction)doneButton:(UIBarButtonItem *)sender
{
NSLog(@"button pressed");

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:self.selectedIndexPathsMutableArray] forKey:@"selectedCells"];
[defaults synchronize];


[[PFUser currentUser] setObject:self.selectedRowsArray forKey:@"interests"];
[[PFUser currentUser] saveInBackground];
}

最佳答案

indexPath 是一个对象,所以你不应该用“==”来测试相等性。也不需要遍历数组,只需要使用containsObject,

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = self.categoriesArray[indexPath.row];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];

if ([self.retrievedIndexPaths containsObject:indexPath]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;

}

关于ios - 无法保存复选标记以显示在 UITableView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29379858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com