2015-10-31 2 views
0

У меня в настоящее время возникает проблема, когда мой collectionView меняет порядок моих предметов, когда они прокручиваются с экрана и переключает выбранную ячейку. В настоящее время они организованы таким образом, что они прокручивается вертикально (и есть две вещи бок о бок):UICollectionView Reorders and Changes Selected Item

enter image description here

Вот мой - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath метод:

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

    static NSString *identifier = @"FriendCell"; 
    FriendsCell *cell = (FriendsCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    //Friend Object 
    PFUser *friend = [self.friendsArray objectAtIndex:indexPath.item]; 
    cell.friendId = currentFriend.objectId; 

    //Setup Cell 
    if (!cell.isSelected) { 
     cell.layer.backgroundColor = [UIColor whiteColor].CGColor; 
    } else { 
     cell.layer.backgroundColor = FlatGreen.CGColor; 
    } 

    //Set Profile Image 
    if (!cell.profileImageView) { 
     cell.profileImageView = [UIImageView new]; 
     cell.profileImageView.image = [UIImage imageNamed:@"ProfileImagePlaceholder"]; 
     [cell addSubview:cell.profileImageView]; 
    } 

    //Set Username 
    if (!cell.usernameLabel) { 
     //Set Username Label 
     cell.usernameLabel = [UILabel new]; 
     cell.usernameLabel.text = friend[@"username"]; 
     cell.usernameLabel.textColor = [UIColor lightGrayColor]; 
     [cell addSubview:cell.usernameLabel]; 

    } else { 
     if (cell.isSelected) { 
      cell.usernameLabel.textColor = [UIColor whiteColor]; 
     } else { 
      cell.usernameLabel.textColor = [UIColor lightGrayColor]; 
     } 
    } 

    return cell; 
} 

didSelectItemAtIndexPath:

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

//Determine the selected friend by using the indexPath 
PFUser *selectedFriend = [self.friendsArray objectAtIndex:indexPath.item]; 

//Add the selected friend's objectId to our array 
[self.friendsToAdd addObject:selectedFriend.objectId]; 

//Show Selected View 
FriendsCell *currentCell = (FriendsCell *)[collectionView cellForItemAtIndexPath:indexPath]; 

//Animate 
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ 

    //Update 
    currentCell.layer.backgroundColor = FlatGreen.CGColor; 
    currentCell.profileImageView.alpha = 0.0; 
    currentCell.usernameLabel.textColor = [UIColor whiteColor]; 

} completion:nil]; 

} 

didDeselectItemAtIndexPath:

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

    //Determine the selected friend by using the indexPath 
    PFUser *deselectedFriend = [self.friendsArray objectAtIndex:indexPath.item]; 

    //Remove the deselected friend's objectId from our array 
    [self.friendsToAdd removeObject:deselectedFriend.objectId]; 

    //Show Deselected View 
    FriendsCell *currentCell = (FriendsCell *)[collectionView cellForItemAtIndexPath:indexPath]; 

    //Animate 
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{ 

     //Update Card 
     currentCell.layer.backgroundColor = [UIColor whiteColor].CGColor; 
     currentCell.profileImageView.alpha = 1.0; 
     currentCell.usernameLabel.textColor = [UIColor lightGrayColor]; 

    } completion:nil]; 
} 

Если кто-нибудь может понять, почему это происходит, я бы очень признателен!

ответ

-2

Попробуйте

dequeueReusableCellWithReuseIdentifier:nil 
+3

'reuseIdentifier' является ненулевым аргументом. Это сбой. – joern

+0

Вы можете указать dequeueReusableCellWithReuseIdentifier: @ "" – Prakash

+0

ОК Теперь я понял. Но я использовал эту концепцию в tableView. – Prakash

 Смежные вопросы

  • Нет связанных вопросов^_^