我正在使用UICollectionView
,并且在复制我在UITableView
中工作的某些内容时遇到了一些困难。我正在设置一个selected
UICollectionView
单元的selectedBackgroundView
,并且每次加载视图时都会保留该选择。当视图重新加载时设置UICollectionViewCell的selectedBackgroundView
我有一个Tab Bar controller
两个选项卡的基本应用程序,其中两个选项卡都是UITableViewControllers
。第二个选项卡是应用内设置,我在那里有两个cells
;一个用于应用程序主题,一个用于键盘主题。键盘主题是另一个Table View
,但App Themes
是一个UICollectionView
与3x4单元网格。虽然这是动态创建的,但不会有更多的单元,这就是它。
工作
现在,我可以选择一个UICollectionViewCell
,并愉快地应用在上面的自定义图像;这工作得很好。
问题
我面临的问题是,当即使值保存在NSUserDefaults
视图重新加载所选单元格不保持在顶部的自定义图像的事实。
因为我有与我的键盘主题(UITableView
)一样的概念,所以我在这里复制了原理,但不知道我所做的事实际上是否正确。
下面是一些代码:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
ThemeCell *themeCell = (ThemeCell *)[self.cView cellForItemAtIndexPath:indexPath];
self.selectedThemeString = themeCell.cellLabel.text;
if(self.checkedIndexPath)
{
UICollectionViewCell *uncheckCell = [self.cView cellForItemAtIndexPath:self.checkedIndexPath];
uncheckCell.selectedBackgroundView = nil;
}
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
self.checkedIndexPath = indexPath;
UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 1, 2)];
dot.image=[UIImage imageNamed:@"check-white-hi.png"];
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
[[NSUserDefaults standardUserDefaults] setObject:self.selectedThemeString forKey:@"Selection"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
在cellForItemAtIndexPath
,我有:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ThemeCell *themeCell = (ThemeCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Theme Cell" forIndexPath:indexPath];
NSString *cellData = [self.themeLabels objectAtIndex:indexPath.row];
themeCell.cellLabel.text = cellData;
themeCell.cellImages.image = self.themeImages[indexPath.row];
UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 1, 2)];
dot.image=[UIImage imageNamed:@"check-white-hi.png"];
self.selectedThemeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"Selection"];
NSLog(@"Selected Theme String = %@", self.selectedThemeString);
if (!self.selectedThemeString)
{
NSLog(@"111");
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
}
if ([self.selectedThemeString isEqualToString:@"Original"])
{
NSLog(@"222");
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
}
if ([self.selectedThemeString isEqualToString:@"Mystical"])
{
NSLog(@"333");
self.checkedIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
}
return themeCell;
我viewWillAppear
有:
self.selectedThemeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"Selection"];
if ([self.selectedThemeString isEqualToString:@"Original"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Peacock"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Mystical"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Zebra"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:3 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Simplicity"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:4 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Rainbow"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:5 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Prosperity"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:6 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Leopard"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:7 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Hypnotic"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:8 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Dunes"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:9 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Twirl"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:10 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Oceanic"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:11 inSection:0];
}
[self.cView reloadData];
细胞的顺序上面定义的,所以第一行第一个单元格被称为原始。第二单元第一行被称为孔雀和第三单元第一行被称为神秘等
我的想法是,我可以定义indexPathForRow
每串并在cellForItemAtIndexPath
,当字符串匹配,它会应用selectedBackgroundView
的图像。
状态
现在所发生的事情是,我可以成功地选择一个单元格,并显示图像;如果我重新加载视图(通过外出并返回),单元格中的selectedBackgroundView
不存在。但是,我知道NSUserDefaults
正在工作,因为在我的cellForItemAtIndexPath
中,“111”或“222”或“333”已成功记录,但它并未设置所选UICollectionCell的高亮状态。
我要求是:
1)有一个选择的单元显示selectedBackgroundView
图像时的视图已被重新加载。
更新: 在cellForItemAtIndexPath
,如果我把themeCell.backgroundView = dot
,而不是themeCell.selectedBackgroundView = dot
,当我重新加载观点,与自定义图像,而不是仅仅所选单元格每一个细胞的亮点,所以我觉得我在进步一些,但仍然输了。
任何指导将非常感激。