2015-06-13 75 views
-1

我可以選擇一個集合視圖中的圖像當我選擇一個圖像時,選擇了錯誤的圖像單元格一旦向下滾動出單元格視圖,備份的細胞不再被選中,我怎樣才能解決這個問題?滾動時單元格顯示不正確

的ImageView的是在故事板定義。 資產都在照片庫中。

這是PhotoCell.h文件。

#import <UIKit/UIKit.h> 
#import <AssetsLibrary/AssetsLibrary.h> 

@interface PhotoCell : UICollectionViewCell 

@property(nonatomic,strong) ALAsset * asset; 
@property (nonatomic,weak) IBOutlet UIImageView * PhotoImageView; 

這是我的PhotoCell.m文件。

#import "PhotoCell.h" 
@interface PhotoCell() 

@end 

@implementation PhotoCell 

#pragma mark - User Made Method 
- (void) setAsset:(ALAsset *)asset 
{ 
    // 2 
    _asset = asset; 
    self.PhotoImageView.image = [UIImage imageWithCGImage:[asset thumbnail]]; 
} 

#pragma mark - CollectionView Cell Method 
-(void)prepareForReuse 
{ 
} 

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

PhotoCell *cell =(PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath]; 
ALAsset * asset = self.assets[indexPath.row]; 
cell.asset = asset; 
cell.backgroundColor = [UIColor redColor]; 
} 

#pragma mark - Collection View Delegate 

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

    //NSLog(@"%@ - %d", NSStringFromSelector(_cmd), indexPath.item);  
    PhotoCell *cell = (PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    chkboxBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [chkboxBtn setFrame:CGRectMake(60, 60, 30, 30)]; 
    [chkboxBtn setTag:100]; 
    [chkboxBtn setImage:[UIImage imageNamed:@"success.png"] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:chkboxBtn ]; 
} 

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

    // This removes the Check Box Button From the Cell After click it again 
    PhotoCell *cell =(PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    [[cell.contentView viewWithTag:100] removeFromSuperview]; 

} 
+1

你可以在你的問題上花更多時間,並嘗試更好地解釋問題嗎?否則,你不會得到任何吻... – tailec

+0

它可能有可能,我需要花一些時間在我的問題,但PLZ試圖理解。 –

回答

1

didSelectItemAtIndexPath您不能添加的複選框,並在didDeselectItemAtIndexPath,刪除它,因爲所有的細胞都會滾動時被重用。

添加複選框,在您的PhotoCell,並在cellForItemAtIndexPath功能,這樣做:

if cell.selected { 
    checkbox.hidden = false 
} 
else { 
    checkbox.hidden = true 
} 
+0

Jacky我不知道該怎麼辦。您能否詳細解釋 –

+0

@vikramsingh在'PhotoCell'中顯示您的代碼。 – Jacky

+0

我是一個新的iPhone很抱歉問你,但PLZ -Jacky,我在故事板中創建了單元格,所以我需要在故事板或cellforItemAtIndexPath中添加chekbox - –

0

你應該知道如何dequeueReusableCellWithIdentifier工作與實現代碼如下細胞。當您撥打[tableView dequeueReusableCellWithIdentifier:]時,它將獲得一個先前已創建並且當前未被使用的單元。這意味着在需要時可以重複使用相同的UITableCell對象。

因此,當您添加複選框按鈕didSelectItemAtIndexPath委託並滾動到底部/向上時,此單元格將重新用於其他單元格索引,並因此顯示單元格被選中。

要解決此問題,您需要將選定的索引路徑存儲在didSelectItemAtIndexPath處,並在didDeselectItemAtIndexPath方法中刪除。

最後,重置[刪除複選框&拇指]單元格cellForItemAtIndexPath數據源,然後將單元格標記爲選中,如果索引路徑包含在選定的索引路徑數組中。

查看下面的代碼,作爲尚未測試過的示例。

從故事板添加一個按鈕,並出口如下:

@interface PhotoCell : UICollectionViewCell 

@property(nonatomic,strong) ALAsset * asset; 
@property (nonatomic,weak) IBOutlet UIImageView * PhotoImageView; 
@property (nonatomic,weak) IBOutlet UIButton * chkboxBtn; 

@end 

更新光電如下:

#import "PhotoCell.h" 

@interface PhotoCell() 

- (void)setCheckBoxSelected:(BOOL)selected 

@end 

@implementation PhotoCell 

#pragma mark - User Made Method 
- (void) setAsset:(ALAsset *)asset 
{ 
    // 2 
    _asset = asset; 
    self.PhotoImageView.image = [UIImage imageWithCGImage:[asset thumbnail]]; 
} 
- (void)setCheckBoxSelected:(BOOL)selected 
{ 
    if (selected) { 
     [self.chkboxBtn setImage:[UIImage imageNamed:@"success.png"] forState:UIControlStateNormal]; 
    } else { 
     [self.chkboxBtn setImage:nil forState:UIControlStateNormal]; 
    } 
} 
@end 

最後修改控制器如下 在你的控制器類添加的NSMutableArray屬性

@property(nonatomic, strong) NSMutableArray *selectedIndexPaths; 

初始化數組at viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.selectedIndexPaths = [NSMutableArray new]; 
} 

然後..

#pragma mark - CollectionView Cell Method 
-(void)prepareForReuse 
{ 
} 

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

    PhotoCell *cell =(PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath]; 
    ALAsset * asset = self.assets[indexPath.row]; 
    cell.asset = asset; 
    cell.backgroundColor = [UIColor redColor]; 

    if ([self.selectedIndexPaths containsObject:indexPath]) { 
     [cell setCheckBoxSelected:NO]; 
    } else { 
     [cell setCheckBoxSelected:YES]; 
    } 
} 

#pragma mark - Collection View Delegate 

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

    [self.selectedIndexPaths addObject:indexPath]; 

    PhotoCell *cell = (PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    [cell setCheckBoxSelected:YES]; 
} 

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

    // This removes the Check Box Button From the Cell After click it again 
    PhotoCell *cell =(PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    [cell setCheckBoxSelected:NO]; 
    [self.selectedIndexPaths removeObject:indexPath]; 
} 

@end 

希望它會工作。

謝謝。

相關問題