我有一个UITable,其数据源设置为原始数据的“副本”。我使用此副本显示过滤结果(“全部”或仅在每行中使用UISwitch“检查”的结果)。我进行过滤的逻辑是:
-(void)filterItems {
[tableData removeAllObjects];
if (checkedOrNotSwitch.selectedSegmentIndex == 0) {
[tableData addObjectsFromArray:self.defaultChecklistItemsArray];
} else {
for (NSMutableDictionary *sectionDict in self.defaultChecklistItemsArray) {
NSMutableArray *newItemsArray = [[NSMutableArray alloc] init];
[newItemsArray removeAllObjects];
for (NSMutableDictionary *itemDict in [sectionDict objectForKey:@"categoryItems"]) {
if ([[itemDict objectForKey:@"isComplete"] boolValue]) {
[newItemsArray addObject:itemDict];
}
}
if ([newItemsArray count] > 0) {
NSMutableDictionary *newSectionDict = [[NSMutableDictionary alloc] initWithDictionary:sectionDict];
[newSectionDict setObject:newItemsArray forKey:@"categoryItems"];
[tableData addObject:newSectionDict];
[newSectionDict release];
}
}
}
[checklistTable reloadData];
}
过滤本身现在可以正常工作。在我的自定义单元格中,每行都有一个UISwitch。交换机在更改时运行此功能:
-(IBAction) switchValueChanged{
NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self];
[self.parentController updateCompletedStatusAtIndexPath:indexPath toStatus:isCompleted.on];
}
上面的函数在tableviewcell本身的类中。我在superview中调用的函数是:
-(void)updateCompletedStatusAtIndexPath:(NSIndexPath *)indexPath toStatus:(BOOL)status{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSMutableDictionary *currentsection = [[NSMutableDictionary alloc] initWithDictionary:[tableData objectAtIndex:section]];
NSMutableArray *itemsArray = [[NSMutableArray alloc] initWithArray:[currentsection objectForKey:@"categoryItems"] copyItems:YES];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[itemsArray objectAtIndex:row]];
NSLog(@"BOOL = %@\n", (status ? @"YES" : @"NO"));
[tempDict setValue:[NSNumber numberWithBool:status] forKey:@"isComplete"];
[itemsArray replaceObjectAtIndex:row withObject:tempDict];
[currentsection setValue:itemsArray forKey:@"categoryItems"];
[tableData replaceObjectAtIndex:section withObject:currentsection];
[tempDict release];
[itemsArray release];
[currentsection release];
[checklistTable reloadData];
}
在我实现过滤并直接在self.defaultChecklistItemsArray上使用上面的逻辑之前,它在翻转开关时工作并保存了数据。
现在,当我第一次进入应用程序时,它会从nsuserdefaults加载数据数组。我导航到带有表格的视图,它会正确显示数据,UISwitch全部位于给定数据的正确位置(一些打开,一些关闭)。当我点击其中一个开关,然后单击进行过滤的分段控制时,数据将恢复到加载状态,这意味着交换机的翻转根本不会影响数据(即使我不知道)我想我在这里的任何地方都做了很深的复制,所以我认为应该做正确的事情。有什么建议吗?