【发布时间】:2015-11-24 12:43:22
【问题描述】:
我有一个简单的应用程序,它由一个 UITabBar 组成,其中每个 Tab 是一个 UITableViewController。出于这个问题的目的,我将只关注名为Videos(第一个选项卡)的UITableViewController 和另一个名为Languages(第二个选项卡)的UITableViewController。
Videos 选项卡由视频列表的一部分组成。 Languages 选项卡包含两个部分,其中section 0 是传单,section 1 = 与视频选项卡相同的相应视频。
例如,如果Videos 选项卡有:
视频 10010
视频 20010
视频 30010
视频 40010
视频 50010
那么Languages tab section 1 也会有:
视频 10010
视频 20010
视频 30010
视频 40010
视频 50010
我有一些代码在已选择的任何单元格上加上一个星号,并且该单元格的标题被添加到 Core Data(将显示在名为 Favourites 的第三个选项卡中 - 但这并不重要对于这个问题)。
我想确保应用程序内的一致性,所以如果我在Videos 选项卡中的Video 20010 中放置一个星号,我想确保语言中的Video 20010 也有一个星号。
那部分有效。然而,问题是星被放置在传单部分 (section 0) 以及 Languages 选项卡的 Videos 部分 (section 1) 中。
这是Languages 标签中cellForRow 的部分代码。
// This code is important because I might set the Leaflets section to be a favourite from within the Languages tab (not related to the Videos tab, etc).
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;
}
}
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");
}
else
{
NSLog(@"Number of objects %lu", [favs count]);
for (Favourites *favourite in favs)
{
NSString *string = [NSString stringWithFormat:@"%@", favourite.title];
favourite.title = string;
NSLog(@"The favourite titles are %@", favourite.title);
if ([customCell.customCellLabel.text isEqualToString:favourite.title])
{
customCell.customCellImage.hidden = NO;
}
}
}
更新
当某个单元格被收藏时,会发生以下情况:
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
CustomLeafletVideoTableViewCell *selectedCell = (CustomLeafletVideoTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
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];
NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Favourites" inManagedObjectContext: context];
request.predicate = [NSPredicate predicateWithFormat:@"title == %@", cellTitle];
NSArray *items = [context executeFetchRequest:request error:&error];
if (error == nil && items.count)
{
NSManagedObject *managedObject = items[0];
[context deleteObject:managedObject];
}
}
[[NSUserDefaults standardUserDefaults] setObject:self.favoritesDict forKey:@"favoritesDict"];
NSError *error = nil;
if (![context save:&error])
{
// Error
}
[cell hideUtilityButtonsAnimated:YES];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
我对将图像放在“语言”选项卡的“传单”部分不感兴趣 (section 0);我只想定位视频部分 (section 1)。使用上面的代码,section 1 中的正确单元格正在应用 UIImageView,但我想确保第一部分(传单)也没有得到星号。
对此的任何指导将不胜感激。
【问题讨论】:
-
当有人喜欢某样东西时,我很难理解你在做什么。看起来你正在向 NSUserDefaults 添加一个字典,但你也得到了一个托管对象,但你没有对它做任何事情。是否有与视频同名的传单?如果是这样,您应该想出一种更好的方法来构建数据。
-
感谢@beyowulf - 传单和视频有不同的标题。我有字典,这样我就可以跟踪最喜欢的行/哪个部分,所以如果我喜欢传单部分中的某些内容,那么视频部分不应该是最喜欢的。 ManagedObjectContext 仅用于将收藏单元格的标题添加到 Core Data。所以这里的问题是,在 cellForRow 中,我让它检查是否有收藏夹,如果有,是哪一行/哪一部分。
-
问题是,当我从“视频”选项卡中收藏视频时,我只希望“语言”中的“视频”部分有收藏,而不是传单。与收藏项目相关的所有内容都在应用程序中运行;这只是这一部分,我想我需要对存储部分等的 NSDictionary 做一些事情。
-
在“视频”选项卡中,视频位于哪个部分?
标签: ios objective-c uitableview uiimageview