0

UICollectionView 님과의 경험이 거의 없지만 100 레벨, 4 가로, 25 세로 아래에 100 개의 버튼이 표시되는 스크롤 가능한 인터페이스를 만들었습니다. 당신이 알고있는대로 즉석에서 생성). 레벨의 타입은 Tortoise입니다. 그래서 당신은 나중에 무엇을 의미하는지 궁금해 할 것입니다. Tortoise의 경우 20 레벨 만 있습니다.Adaptable UICollectionViewCell 배경 이미지 - 잠긴/잠금 해제 된 게임 레벨

바로 지금 숫자를 표시하는 데 사용하는 셀 데이터 문자열이 셀의 일반 배경 위에 배치됩니다 (레벨이 잠금 해제되었지만 완료되지 않았 음을 나타냄).

배경 이미지로 사용하고자하는 다른 두 개의 이미지가 있습니다 (하나는 숫자 문자열이 나타나지 않는 잠금 이미지이고 다른 하나는 위와 동일한 배경으로 완성을위한 작은 체크 표시가 있습니다 (숫자 위에 문자열)).

간단히 요약하면, 코어 데이터를 사용하여 레벨이 잠겨 있는지 또는 잠금 해제되어 있는지와 함께 몇 가지 개체 세트를 추적합니다. 내가 figureOutLocks이라는 개체를이 배열 self.lockArray에 저장하는 메서드 (바로 Tortoise UICollectionView로 입력)를 호출합니다. 이 방법은 다음과 같습니다.

- (void)figureOutLocks { 
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     NSManagedObjectContext* managedObjectContext = [(AppDelegate*)[[UIApplication sharedApplication] 
                    delegate] managedObjectContext]; 
     NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Score" inManagedObjectContext:managedObjectContext]; 
     [fetchRequest setEntity:entityDescription]; 
     for (int i = 0; i < 20; i++) { 
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %d AND %K == %d", 
             @"level", i, @"typeOfLevel", 0]; 
      [fetchRequest setPredicate:predicate]; 
      NSError *error = nil; 
      Score* lockInfo = [[managedObjectContext executeFetchRequest:fetchRequest 
                    error:&error] lastObject]; 

      lockInfo.levelCompleted = [lockInfo valueForKey:@"levelCompleted"]; 
      lockInfo.lockedLevel = [lockInfo valueForKey:@"lockedLevel"]; 
      NSInteger complete = [lockInfo.levelCompleted integerValue]; 
      NSInteger locked = [lockInfo.lockedLevel integerValue]; 

      if ((locked == 0) && (complete == 0)) { 
       // level is unlocked but not complete (does not have any saved scores) 
       // lockArray gets a 0 
       [self.lockArray addObject:[NSNumber numberWithInteger:0]]; 
      } else if ((locked == 1) && (complete == 0)) { 
       // level is locked which implies it is not complete 
       // lockArray gets a 1 
       [self.lockArray addObject:[NSNumber numberWithInteger:1]]; 
      } else if ((locked == 0) && (complete == 1)) { 
       // level is complete thus it is unlocked 
       // lockArray gets a 2 
       [self.lockArray addObject:[NSNumber numberWithInteger:2]]; 
      } 
     } 
    } 

다시 한 번 요약하면 첫 번째 수준은 잠금 해제되고 완료되지 않으며 다른 수준은 잠겨 (따라서 완료되지 않음) 나타납니다.

또한 에 문자열 개체 1-20이 포함되어 있고 NSArray *compareArray에는 NSNumber 개체 1-20이 들어 있습니다. 내 lockArrayNSMutableArray입니다.

또한 일반 배경과 잠긴 배경을 모두 사용하기 위해 두 개의 별도 UICollectionViewCell 하위 클래스를 만들기로했습니다. 잠긴 배경이 제대로 작동하는지 확인하기 위해 완성 배경 서브 클래스를 추가하지 않았습니다.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
      cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

     UICollectionViewCell *regular; 

     static NSString *cellIdentifier = @"tortoiseCell"; 
     static NSString *tortIdentifier = @"tortoiseLocked"; 
     TortoiseCell *cell = (TortoiseCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
     TortoiseLocked *lockCell = (TortoiseLocked *)[collectionView dequeueReusableCellWithReuseIdentifier:tortIdentifier forIndexPath:indexPath]; 

     NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section]; 
     NSString *cellData = [data objectAtIndex:indexPath.row]; 
     NSMutableArray *locks = [self.compareArray objectAtIndex:indexPath.section]; 
     NSNumber *locksData = [locks objectAtIndex:indexPath.row]; 
     NSInteger locked = [locksData integerValue]; 
     NSInteger lock = [[self.lockArray objectAtIndex:locked] integerValue]; 

     if (lock == 0) { 
      [cell.buttonClick setTag:indexPath.row]; 
      [cell.buttonClick setTitle:cellData forState:UIControlStateNormal]; 
      [cell.buttonClick setBackgroundImage:[UIImage imageNamed:@"TortoiseLevels.png"] 
           forState:UIControlStateNormal]; 
      [cell.buttonClick setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
      [cell.buttonClick.titleLabel setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:25]]; 
      [cell.buttonClick addTarget:self action:@selector(buttonPressedSoWhatNumber:) 
       forControlEvents:UIControlEventTouchUpInside]; 
      cell.buttonClick.layer.cornerRadius = 8; 
      cell.buttonClick.layer.masksToBounds = YES; 
      [cell addSubview:cell.buttonClick]; 
      cell.layer.shouldRasterize = YES; 
      cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 
      regular = cell; 
     } else if (lock == 1) { 
      [lockCell.tortoiseLock setTag:indexPath.row]; 
      [lockCell.tortoiseLock setBackgroundImage:[UIImage imageNamed:@"TortoiseLock.png"] 
           forState:UIControlStateNormal]; 
      lockCell.tortoiseLock.layer.cornerRadius = 8; 
      lockCell.tortoiseLock.layer.masksToBounds = YES; 
      [lockCell addSubview:lockCell.tortoiseLock]; 
      lockCell.layer.shouldRasterize = YES; 
      lockCell.layer.rasterizationScale = [UIScreen mainScreen].scale; 
      regular = lockCell; 
     } 

     return regular; 
    } 

내가도 가능 한 일이다 :

여기에 주요 방법입니까? 이 작품을 만들려면 어떻게해야합니까? 하나의 UICollectionViewCell 서브 클래스를 사용해 보았지만 프로그래밍 방식으로 배경을 변경했지만 작동하지 않았기 때문에 더 큰 if 문으로 볼 수 있습니다. 이견있는 사람?

답변

2

테이블에서 셀을 모두 꺼내지 않아도됩니다. 그 중 하나만 사용하게됩니다. 코드는 더 같이 구성해야합니다

이미 스토리 보드에 전지 프로토 타입의 두 가지 유형을 설정 한 이후 또한
static NSString *cellIdentifier = @"tortoiseCell"; 
static NSString *tortIdentifier = @"tortoiseLocked"; 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
       cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    UICollectionViewCell *regular; 

    NSMutableArray *locks = [self.compareArray objectAtIndex:indexPath.section]; 
    NSNumber *locksData = [locks objectAtIndex:indexPath.row]; 
    NSInteger locked = [locksData integerValue]; 

    NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section]; 
    NSString *cellData = [data objectAtIndex:indexPath.row]; 
    NSInteger lock = [[self.lockArray objectAtIndex:locked] integerValue]; 

    if (lock == 0) { 

     TortoiseCell *cell = (TortoiseCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
     [cell.buttonClick setTag:indexPath.row]; 
     [cell.buttonClick setTitle:cellData forState:UIControlStateNormal]; 
     [cell.buttonClick setBackgroundImage:[UIImage imageNamed:@"TortoiseLevels.png"] 
            forState:UIControlStateNormal]; 
     [cell.buttonClick setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     [cell.buttonClick.titleLabel setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:25]]; 
     [cell.buttonClick addTarget:self action:@selector(buttonPressedSoWhatNumber:) 
        forControlEvents:UIControlEventTouchUpInside]; 
     cell.buttonClick.layer.cornerRadius = 8; 
     cell.buttonClick.layer.masksToBounds = YES; 
     [cell addSubview:cell.buttonClick]; 
     cell.layer.shouldRasterize = YES; 
     cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 
     regular = cell; 

    } else if (lock == 1) { 

     TortoiseLocked *lockCell = (TortoiseLocked *)[collectionView dequeueReusableCellWithReuseIdentifier:tortIdentifier forIndexPath:indexPath]; 
     [lockCell.tortoiseLock setTag:indexPath.row]; 
     [lockCell.tortoiseLock setBackgroundImage:[UIImage imageNamed:@"TortoiseLock.png"] 
             forState:UIControlStateNormal]; 
     lockCell.tortoiseLock.layer.cornerRadius = 8; 
     lockCell.tortoiseLock.layer.masksToBounds = YES; 
     [lockCell addSubview:lockCell.tortoiseLock]; 
     lockCell.layer.shouldRasterize = YES; 
     lockCell.layer.rasterizationScale = [UIScreen mainScreen].scale; 
     regular = lockCell; 
    } 

    return regular; 
} 

는 왜 프로그래밍 각 셀을 설정하는 귀찮게? 스토리 보드에서 원하는 모양을 보이게하고 코드에 레벨 번호를 추가하십시오.

+0

훌륭한 솔루션입니다. 또한'compareArray'의 객체 중 하나를 사용하여'lockArray'에있는 객체의 값을 평가하는 과정이 매우 잘못되었습니다. 그리고 모든 작업이 끝나면 펜촉과 스토리 보드를 통해 모든 것을 변환하려고 노력할 것입니다. 나는 코드가 모두 배치 된 것을보고 싶다. –