我将plist加载到NSArry,过滤该数组以捕获我需要的数据,然后将数据呈现给UITableView。这很好用。
修改
我正在使用字典数组。 Plist充满了一系列词典。
这就是我的所作所为:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/data.plist"];
//array is filled with dictionaries
array = [NSMutableArray arrayWithContentsOfFile:filePath];
//filter the array to catch appropriate data (there are maybe 10 objects):
self.arrayFiltered = [NSMutableArray arrayWithCapacity:[array count]];
NSDictionary *dict;
for (dict in array)
if ([[dict valueForKey:@"globalKey"] isEqualToNumber:[NSNumber numberWithInt:year]])
[arrayFiltrirani addObject:dict];
现在我使用arrayFiltered中的数据填充tableView。现在我想从tableView和arrayFiltered中删除一行。
[self.arrayFiltrirani removeObjectAtIndex:indexPath.row];
BUT !!!如何更新原始数组并将其保存到plist?
答案 0 :(得分:1)
由于你正在使用字典,你需要将实际的字典容器对象写回data.plist文件,所以
NSDictionary *myDictionary = [masterArray objectAtIndex:indexOfDictionary];
NSMutableArray *myDataArray = [myDictionary objectForKey:@"dataArray"];
[myDataArray removeObject:objRemovedFromTable]; //or removeObjectAtIndex:indexPath.row
NSString *filePath = [docsDirectory stringByAppendingPathComponent:@"data.plist"];
[masterArray writeToFile:filePath atomically:YES];
答案 1 :(得分:1)
我设法解决了我的问题。基本上我从主数组中创建了2个数组,“Filtered”数组和“Other”数组。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/data.plist"];
array = [NSMutableArray arrayWithContentsOfFile:filePath];
self.arrayFiltered = [NSMutableArray arrayWithCapacity:[array count]];
self.arrayOther = [NSMutableArray arrayWithCapacity:[array count]];
NSDictionary *dict;
for (dict in array)
if ([[dict valueForKey:@"globalKey"] isEqualToNumber:[NSNumber numberWithInt:godinaMjesec]])
[arrayFiltered addObject:dict];
else {
[arrayOther addObject:dict];
}
现在,当我想从arrayFiltered中删除数据时(tableView从中获取数据):
[self.arrayFiltered removeObjectAtIndex:indexPath.row];
要写原始数组:
[self.arrayOther addObjectsFromArray:self.arrayFiltered];
[self.arrayOther writeToFile:filePath atomically:YES];
这可能不是一种正确的方法,但它对我有用。
答案 2 :(得分:0)
如果它是一个需要一段时间才能写入的大plist文件,那么您可能正在使用错误的工具。我会使用Core Data来删除单个数据行。