在不同的UITableViewControllers中的UITableViewCells上一致使用UIImageView

我有一个简单的4-tab UITabBarController ,每个选项卡都与UITableViewController一致。 第一个选项卡称为Videos ,第二个选项卡是Languages ,第三个是Favourites ,第四个是Leaflets 。 “ Videos选项卡包含语言列表(大约6个),单击单元格时,将执行segue以显示该语言的可用video。 “ Languages选项卡是应用程序中所有语言的列表。 单击单元格时,将显示该语言的可用传单和video。 使用Core Data填充Favourites (我将回到Leaflets ),最后, Leaflets选项卡是一个语言列表; 当您单击某种语言时,您会看到该语言提供的传单。

数据,单元格内容,传单和video都是通过prepareForSegue NSMutableArray填充的。 该应用程序运行良好。

在此问题的指导下: 在事件发生时更改特定UITableViewCell的UIImage ,我可以在“ Languages选项卡中点击一个单元格,它可以执行以下两项操作:

1)向单元格添加星形UIImageView

2)将该单元格的标题添加到Core Data

Favourites选项卡使用NSFetchedResultsController来填充它的UITableViewCell ,其中包含已被优惠的单元格的标题。

那部分工作得很好。

问题

但是,我现在想实现一致性。 我的意思是,如果用户在Languages选项卡中选择“英语”并collections一个名为“3 Facts”的video,当他们进入Videos选项卡并单击英语时,“3 Facts” cell应该有一个最喜欢的在上面。

Languages选项卡是唯一一个显示语言后的UITableViewController ,它显示了一个UITableViewController其中包含第0部分的Leaflets和第1部分的video。如果用户进入Videos选项卡并选择语言,它只包含一个部分。

这是目前正在做的最受欢迎的代码:

 - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; CustomLeafletVideoTableViewCell *selectedCell = (CustomLeafletVideoTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath]; // We create a new NSString and assign it to the custom label's text (which is obtained from the cellForRow method) NSString *cellTitle = selectedCell.customCellLabel.text; NSLog(@"The text is %@", cellTitle); NSManagedObjectContext *context = [self managedObjectContext]; NSString *key = [NSString stringWithFormat:@"%@_%ld_%ld", self.selectedLanguage, (long)indexPath.section, (long)indexPath.row]; if (self.favoritesDict[key] == nil) { self.favoritesDict[key] = @(1); Favourites *favourites = [NSEntityDescription insertNewObjectForEntityForName:@"Favourites" inManagedObjectContext:context]; favourites.title = cellTitle; } else { [self.favoritesDict removeObjectForKey:key]; } [[NSUserDefaults standardUserDefaults] setObject:self.favoritesDict forKey:@"favoritesDict"]; NSError *error = nil; if (![context save:&error]) { // Error } [cell hideUtilityButtonsAnimated:YES]; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 

viewDidLoadviewWillAppear包含:

 if ([[NSUserDefaults standardUserDefaults] valueForKey:@"favoritesDict"] == nil) { [[NSUserDefaults standardUserDefaults] setObject:@{} forKey:@"favoritesDict"]; } // load favorites from user defaults self.favoritesDict = [[[NSUserDefaults standardUserDefaults] valueForKey:@"favoritesDict"] mutableCopy]; 

cellForRowAtIndexPath包含:

 if(indexPath.section==0) { customCell.customCellLabel.text = [NSString stringWithFormat:@"%@",[self.availableLeaflets objectAtIndex:indexPath.row]]; NSString *key = [NSString stringWithFormat:@"%@_%ld_%ld", self.selectedLanguage, (long)indexPath.section, (long)indexPath.row]; if (self.favoritesDict[key]) { // show the favorite image customCell.customCellImage.hidden = NO; } else { // hide the favorite image customCell.customCellImage.hidden = YES; } } else { customCell.customCellLabel.frame = CGRectMake(8, 20, 100, 40); customCell.customCellLabel.text = [NSString stringWithFormat:@"%@",[self.availableVideos objectAtIndex:indexPath.row]]; NSString *key = [NSString stringWithFormat:@"%@_%ld_%ld", self.selectedLanguage, (long)indexPath.section, (long)indexPath.row]; if (self.favoritesDict[key]) { // show the favorite image customCell.customCellImage.hidden = NO; } else { // hide the favorite image customCell.customCellImage.hidden = YES; } } 

NSString *key在此处起作用,因为它正在获取正确的节和行。 当我将此代码复制到“ Videos选项卡时,collections夹与“ Languages选项卡在同一标题上不存在。

最终,一致性是重要的事情。 用户可能会从“video”选项卡或“语言”选项卡中collections某些内容,但不管怎样,相同的“标题”应该在其他选项卡中受欢迎。

更新

根据使用NSUserDefaults的评论,我添加了一些代码。 首先,在LanguagesTab中,我在方法中添加了一些代码来执行favouriting:

 NSString *key = [NSString stringWithFormat:@"%@_%ld_%ld", self.selectedLanguage, (long)indexPath.section, (long)indexPath.row]; if (self.favoritesDict[key] == nil) { self.favoritesDict[key] = @(1); Favourites *favourites = [NSEntityDescription insertNewObjectForEntityForName:@"Favourites" inManagedObjectContext:context]; favourites.title = cellTitle; NSString *valueToSave = cellTitle; NSLog(@"The valuetosave is %@", cellTitle); [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"starredName"]; [[NSUserDefaults standardUserDefaults] synchronize]; } 

回到VideosTab ,在cellForRow ,我有:

 NSString *key = [NSString stringWithFormat:@"%@_%ld_%ld", self.selectedLanguage, (long)indexPath.section, (long)indexPath.row]; if (self.favoritesDict[key]) { // show the favorite image customCell.customCellImage.hidden = NO; } else { // hide the favorite image customCell.customCellImage.hidden = YES; } NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"starredName"]; if ([savedValue isEqualToString:@"# 3 The One Show"]) { NSLog(@"Display star"); customCell.customCellImage.hidden = NO; } else { NSLog(@"Don't display star"); } 

这在某种程度上起作用。 例如,我在“语言”选项卡中选择了一个名为#3 The One Show的单元格,当我回到“ Videos选项卡时,它确实在if语句中检测到它并运行NSLog以显示星形。 然后显示星星。 然而,问题是,它显示了每个细胞上的明星。 我希望它只在单元格上显示一个带有“#3 The One Show”文本的明星。

我试过了:

 NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"starredName"]; if ([savedValue isEqualToString:@"# 3 The One Show"]) { if ([customCell.customCellLabel.text isEqualToString:@"# 3 The One Show"]) { NSLog(@"Display star"); customCell.customCellImage.hidden = NO; } } else { NSLog(@"Don't display star"); } 

这看起来像是在工作,现在显示并且不显示单元格。 但是,这与savedValue中的1个值一起使用。 在cellForRow中,如果我有:

 NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"starredName"]; if ([savedValue isEqualToString:@"# 3 The One Show"]) { NSLog(@"Display star"); NSLog(@"The cell is %@", customCell.customCellLabel.text); if ([customCell.customCellLabel.text isEqualToString:@"# 3 The One Show"]) { customCell.customCellImage.hidden = NO; } } else if ([savedValue isEqualToString:@"Real Talk - Sangat TV discussion #TruthAbout1984"]) { NSLog(@"Display the star yo"); NSLog(@"The title is %@", customCell.customCellLabel.text); if ([customCell.customCellLabel.text isEqualToString:@"Real Talk - TV discussion"]) { customCell.customCellImage.hidden = NO; } } else { NSLog(@"Don't display star"); } 

它只显示savedValue NSUserDefaults的最新条目上的savedValue NSUserDefaults ,因为我有1000多个条目,实际为每个不同的条目创建不同的密钥效率不NSUserDefaults

现在,因为我的Favourites UITableViewController正在由Core Data填充,而UITableViewCells正由favourites.title填充,我能用它做点什么吗? 所以,我已经拥有了受欢迎的标题的价值,但是没有办法在“video”标签中进行翻译。 或许可以获取获取标题的获取请求等? 我只需要一些指导。

我有这个代码:

 Favourites *favourite = nil; NSManagedObjectContext *context = [self managedObjectContext]; NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Favourites"]; request.predicate = [NSPredicate predicateWithFormat:@"title != nil"]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]; request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSError *error = nil; NSArray *favs = [context executeFetchRequest:request error:&error]; if (!favs) { NSLog(@"Nothing to see here"); // Handle Error } else { // If the object exists, just return it // favourite = [favs lastObject]; NSLog(@"Number of objects %lu", [favs count]); NSString *elements; for (elements in favs) { NSLog(@"Elements %@", elements); } NSLog(@"Things to see here"); } NSLog(@"The Favourites are %@", favourite.title); 

这更像是在黑暗中刺伤 – 我不知道如何返回数组的所有对象,然后使用customCell.customCellImage.hidden = NO等将图像放在该单元格上。

任何关于此的指导都将非常感激。