Ios7 从NSUserDefaults查找数组的索引路径

Ios7 从NSUserDefaults查找数组的索引路径,ios7,nsmutablearray,uicollectionview,nsuserdefaults,Ios7,Nsmutablearray,Uicollectionview,Nsuserdefaults,我有一个UICollectionView,我使用它作为过滤器,允许用户直观地选择他们想要浏览的类别。当他们选择或取消选择这些类别时,我将这些类别名称放入一个可变数组中,并将它们存储在NSUserDefaults中 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSString *selectedCategory

我有一个UICollectionView,我使用它作为过滤器,允许用户直观地选择他们想要浏览的类别。当他们选择或取消选择这些类别时,我将这些类别名称放入一个可变数组中,并将它们存储在NSUserDefaults中

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

     NSString *selectedCategory = [categoryNames objectAtIndex:indexPath.row];
     [selectedCategories addObject:selectedCategory];

     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
     [userDefaults setObject:selectedCategories forKey:@"selectedCategory"];
     [userDefaults synchronize]; }


- (void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

     NSString *deSelectedCategory = [categoryNames objectAtIndex: indexPath.row];
     [selectedCategories removeObject:deSelectedCategory];

     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
     [userDefaults setObject:selectedCategories forKey:@"selectedCategory"];
     [userDefaults synchronize]; }
当用户返回到filters屏幕时,我希望在输入时再次将他们在UICollectionView中选择的类别预先填充为selected。我已经能够使用以下代码收回存储在NSUserDefaults中的数据:

NSMutableArray *selectedCategories = [[userDefaults objectForKey:@"selectedCategory"]mutableCopy];
我很难弄清楚的是,如何使用从NSUserDefaults中提取的数组与categoryNames数组进行比较,categoryNames数组将所有类别名称列为NSString,并用于填充cellForItematIndexPath中的UICollectionView,并找到相应的indexpath,然后预选择正确的UICollectionView单元格

似乎需要-indexofObject,但我一直在努力正确使用它


我对ios编程还是相当陌生的,所以任何代码示例都将不胜感激

假设您正在加载选定类别名称的列表,然后再显示任何单元格,例如在viewDidLoad中。在这种情况下,您只需要向cellForRowAtIndexPath添加一些逻辑,以选择类别名称位于selectedCategories中的单元格。它可能看起来像这样:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = ...;

    NSString *category = self.categoryNames[indexPath.item];
    if ([self.selectedCategories containsObject:category] && !cell.selected) {
        [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        // There seems to be a bug in UICollectionView such that the previous
        // statement doesn't trigger the cell's selected view to be displayed.
        // Adding the following statement fixes the issue. Keeping the above
        // statement because it is important when allowsMultipleSelection is NO.
        cell.selected = YES;
    }
}

在试用了您的代码并运行了一些NSLogs之后,当我从“viewDidLoad”中的NSUserDefaults创建selectedCategories数组时,似乎一切都很正常。但当我使用上面的代码示例在cellForRowAtIndexPath中调用它时,selectedCategories数组显示为空。你有没有想过为什么会这样?我的代码有输入错误。selectedCategories需要是一个属性或实例变量。这很有效,selectedCategories正在通过编程进行传递和选择,但现在有一个奇怪的接口怪癖,我无法理解。当用户重新进入页面时,使用上面显示的代码选择的单元格不会在界面中显示为选中。当前,在用户选择另一个类别之前,我会在选择时更改背景颜色,然后使用背景颜色改变。关于为什么会发生这种情况,有什么想法吗?目前,我在选择时有背景颜色的更改。请显示用于突出显示选定单元格的代码。我用于突出显示选定单元格的代码位于cellForItemAtIndexPath UIView*selectedBGView=[[UIView alloc]initWithFrame:cell.bounds]中;selectedBGView.backgroundColor=[UIColor redColor];cell.selectedBackgroundView=selectedBGView;