1

我正在使用UICollectionView,并且在复制我在UITableView中工作的某些内容时遇到了一些困难。我正在设置一个selectedUICollectionView单元的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,当我重新加载观点,与自定义图像,而不是仅仅所选单元格每一个细胞的亮点,所以我觉得我在进步一些,但仍然输了。

任何指导将非常感激。

回答

0

已避免的情况。

我修复了这个问题。

作为参考,我将把这里的固定代码放在任何遇到同样问题的人身上。

cellForItemAtIndexPath,我做了以下内容:

if (!self.selectedThemeString) 
    { 
     NSLog(@"111"); 

     self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
     themeCell.backgroundView = dot; 
     [themeCell addSubview:dot]; 
    } 

    if([self.checkedIndexPath isEqual:indexPath]) 
    { 
     NSLog(@"Loaded"); 
     themeCell.backgroundView = dot; 
     [themeCell addSubview:dot]; 

    } 
    else 
    { 
     NSLog(@"Not Loaded"); 

     themeCell.backgroundView = nil; 
    } 

这是类似的代码前面,但是从我UITableView代码检查indexPath基本复制。

另一个变化是我使用backgroundView属性而不是selectedBackgroundView

我在我的课上做了这个改变,所以无论我有没有selectedBackgroundView,我都将它改为了backgroundView。

我很高兴,这工作就像一种享受。

相关问题