2013-06-04 23 views
0

我也必須問這個問題。已經查看了幾個小時的代碼並嘗試了一切,但爲什麼我的舊圖片沒有被取消選擇?用戶應該只能選擇一個圖標。所選圖標保存在覈心數據庫中。所以當打開這個視圖時,這個圖標也被預先選中。然而,當他選擇一個新的圖標時,這個項目不會被取消選擇。爲什麼?收藏查看舊圖標沒有被取消選中

#import "IconSelectionCollectionViewController.h" 
#import "IconSelectionCell.h" 

@interface IconSelectionCollectionViewController() 
@property (nonatomic, strong) NSArray *icons; 
@end 


@implementation IconSelectionCollectionViewController 

@synthesize mainCategory = _mainCategory; 


#pragma mark Initialize model 
- (void)setMainCategory:(MainCategory *)mainCategory 
{ 
    //TODO 
    _mainCategory = mainCategory; 
    self.title = mainCategory.name; 
} 


#pragma mark View setup 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self.collectionView registerClass:IconSelectionCell.class forCellWithReuseIdentifier:@"IconCell"]; 

    // Do any additional setup after loading the view. 
    self.icons = [NSArray arrayWithObjects:@"DefaultIcon", @"Car", @"Diploma", @"Earth", @"Flight", @"Home", @"Pen", @"Scooter", @"Ship", @"Train", nil]; 
    self.collectionView.allowsSelection = YES; 
    self.collectionView.allowsMultipleSelection = NO; 
} 


#pragma mark Data source delegate methods 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return self.icons.count; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"IconCell"; 

    IconSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    cell.iconImageView.image = [UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]]; 

    if([self.mainCategory.icon isEqualToString:[self.icons objectAtIndex:indexPath.row]]){ 
     cell.selected = YES; 
    } 

    return cell; 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

#pragma mark Collection View Delegate methods 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.mainCategory.icon = [self.icons objectAtIndex:indexPath.row]; 
} 

#pragma mark – UICollectionViewDelegateFlowLayout 

- (CGSize)collectionView:(UICollectionView *)collectionView layout: 
(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    CGSize itemSize; 
    itemSize.height = 62; 
    itemSize.width = 62; 

    return itemSize; 
} 

- (UIEdgeInsets)collectionView: 
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 
    return UIEdgeInsetsMake(10, 10, 10, 10); 
} 

@end 

回答

0

我不是難以置信熟悉收集的意見,但他們都非常相似,表視圖,這是有時UITableviews出現問題,所以我將通過幾個可能的解決方案工作。

執行此操作的一種方法是在選擇代碼後調用[self.collectionView reloadData]。這應該會導致collectionView重新繪製圖像,只將其中的一個設置爲選定的圖像。

但是,我不確定這會解決問題,因爲出隊可重用單元可能會導致爲重用單元設置「選擇」值。或者,我想你可以使用UICollectionView的方法cellForItemAtIndexPath獲取兩個單元格,然後按照您在主要方法中設置它們的方式設置它們的選定值。這可能只是工作,但如果沒有,嘗試這樣做,並再次調用重新加載數據。