1

UICollectionView으로 작업 중이며 UITableView에서 작업 한 내용을 복제하는 데 약간의 어려움이 있습니다. selectedUICollectionView 셀의 selectedBackgroundView을 설정하고보기가로드 될 때마다 해당 선택 항목을 유지하려고합니다.뷰가 다시로드 될 때 UICollectionViewCell의 selectedBackgroundView 설정

두 개의 탭이 UITableViewControllersTab Bar controller에 두 개의 탭이있는 기본 응용 프로그램이 있습니다. 두 번째 탭은 인앱 설정이고 두 개는 cells입니다. 하나는 앱 테마 용이고 다른 하나는 키보드 테마 용입니다. 키보드 테마는 또 다른 Table View이지만 App Themes은 격자가 3x4 인 UICollectionView입니다. 이것이 동적으로 생성되었지만 더 이상 셀이 존재하지 않을 것입니다. 지금

근무

, 나는 UICollectionViewCell를 선택하고 행복하게 상단에 사용자 정의 이미지를 적용 할 수 있습니다; 그것은 아주 잘 작동합니다.

문제

내가 직면하고 문제는보기가 값이 NSUserDefaults에 저장하더라도 다시로드 할 때 선택한 셀 상단에서 해당 사용자 정의 이미지를 유지하지 않는다는 사실이다.

키보드 테마 (UITableView)와 동일한 개념을 사용하고 있기 때문에 여기에서 원칙을 복사했지만 실제로 한 것이 맞는지 확실하지 않습니다. 여기

가 일부 코드의 다음 cellForItemAtIndexPath에서

- (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]; 
} 

, 내가 가진 :

- (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이 존재하지 않습니다. 그러나 cellForItemAtIndexPath에서 "111"또는 "222"또는 "333"이 성공적으로 기록되기 때문에 NSUserDefaults이 작동하고 있음을 알고 있습니다. 그러나 선택한 UICollectionCell의 강조 표시 상태를 설정하지 않았기 때문입니다.

1) 뷰가 다시로드되었을 때 선택된 셀이 selectedBackgroundView 이미지를 표시 유무 :

는 내가 필요로하는 것은이다.

업데이트 : cellForItemAtIndexPath에서 , 내가보기를 다시로드 할 때, 대신 선택한 셀의 사용자 정의 이미지와 모든 단일 세포 하이라이트, 그래서 내가 어느 정도 진전을 보이고 있다고 생각 대신 themeCell.selectedBackgroundView = dotthemeCell.backgroundView = 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; 
    } 

이 코드 이전과 비슷하지만 본질적으로 indexPath 확인 내 UITableView 코드에서 복사.

다른 변경 사항은 selectedBackgroundView이 아닌 backgroundView 속성을 사용한다는 것입니다.

나는이 클래스를 통해이 변경 작업을 수행 했으므로 selectedBackgroundView가 있던 곳이면 backgroundView로 변경했습니다.

나는 행복하고 치료법처럼 작동합니다.

관련 문제