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。

我很高興,這工作就像一種享受。

相關問題