我有 2 个表视图,第一个是主表,第二个是收藏夹表。
一切都用 plist 处理。
在第一个表的 detailView 中,在右上角,我创建了一个按钮,该按钮将那个单元格的值 YES 或 NO 写入 plist,以便该特定单元格将显示在表收藏夹中。
现在的问题是:
- 我从第一个表中选择一行,即主表。detailView 出现,我点击 UIBarButton 使该细节成为收藏夹(因此星形图像作为图像出现在按钮中,让用户知道该单元格是收藏夹)。好的。
- 我去第二张表,最喜欢的一张,选择以前收藏的单元格,我看到星号是空的,然后布尔值设置为 NO(它应该设置为 YES,因为我刚才收藏了它)。
我注意到,如果我将firstTable的第一项添加到收藏夹中,那么即使在我将它们全部收藏在第一个视图中之后,第二个视图中的其他单元格也会正确显示。
有点棘手,我知道,但它正在发生。
知道为什么吗?
我用于读取 plist 的方法如下:
- (NSArray*)readPlist
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"tipsList.plist"];
NSFileManager *fMgr = [NSFileManager defaultManager];
if (![fMgr fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
}
NSMutableArray *returnArr = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isFav == YES"];
for (NSDictionary *sect in returnArr) {
NSArray *arr = [sect objectForKey:@"Rows"];
[sect setValue:[arr filteredArrayUsingPredicate:predicate] forKey:@"Rows"];
}
return returnArr;
[self.tableView reloadData];
}
cellForRow:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *rows;
if (tableView == self.searchDisplayController.searchResultsTableView) {
rows = filteredList;
} else {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isFav == YES"];
NSDictionary *section = [localSortedTips objectAtIndex:indexPath.section];
rows = [section objectForKey:@"Rows"];
[rows filteredArrayUsingPredicate:predicate];
}
cell.textLabel.text = [[rows objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}
和 didSelectRow:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ NSDictionary *section = [localList objectAtIndex:indexPath.section];
NSArray *rows = [section objectForKey:@"Rows"];
NSDictionary *item = [rows objectAtIndex:indexPath.row];
detailViewController *detail = [[detailViewController alloc] initWithNibName:@"detailViewController" bundle:nil];
detail.indexPath = indexPath;
detail.imageString = [item valueForKey:@"image"];
detail.nameString = [item valueForKey:@"name"];
detail.title = [item valueForKey:@"name"];
detail.descriptionString = [item valueForKey:@"description"];
[self.navigationController pushViewController:detail animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[detail release];
}
}
用图片编辑
可以,然后呢。我选择mainTable第一部分的 第一行,出现detailView:
然后我通过那个小星星收藏它,在plist中将它的值设置为YES
然后我进入收藏夹表,点击以前收藏的元素,它看起来像这样
好的。这行得通。但仅限于此!
事实上,如果我尝试选择主表中间的一行,并尝试收藏它,detailView 看起来像
但是,在收藏夹表中选择它看起来像这样,空星,而它应该被收藏然后填充。如果我尝试通过星号收藏它,然后返回收藏夹表,则 mainTable 中的前一行会出现,即使它没有被收藏。
我注意到如果我保留第一个元素,它工作正常,但如果我删除它,它就不再工作了。:\
任何帮助都非常感谢,从 2 周开始。