![](/img/trans.png)
[英]UICollectionView - didDeselectItemAtIndexPath not called if cell is selected
[英]Image of cell does not change in didDeselectItemAtIndexPath
我嘗試在UICollectionView
對象的單元格中更改圖像的狀態...
這是我的代碼的一部分:
-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
cell *aCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
[aCell.myImage setImage:[UIImage imageWithContentsOfFile:self.imageArray[indexPath.item]]];
if (aCell.selected) {
aCell.myImage.layer.borderWidth = 1.0;
aCell.myImage.layer.borderColor = [UIColor blackColor].CGColor;
aCell.mySelect.hidden = NO;
} else {
aCell.myImage.layer.borderWidth = 0.0;
aCell.myImage.layer.borderColor = nil;
aCell.mySelect.hidden = YES;
}
return aCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Select");
cell *selectedCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
if (selectedCell.selected) {
[selectedCell.mySelect setImage:[UIImage imageNamed:@"select.png"]];
selectedCell.myImage.layer.borderWidth = 1.0;
selectedCell.myImage.layer.borderColor = [UIColor blackColor].CGColor;
selectedCell.mySelect.hidden = NO;
} else {
[selectedCell.mySelect setImage:nil];
selectedCell.myImage.layer.borderWidth = 0.0;
selectedCell.myImage.layer.borderColor = nil;
selectedCell.mySelect.hidden = YES; }
}
當我點擊任何單元格時,值選擇會更改,但視圖對象不會刷新。
您需要從didSelectItemAtindexPath方法刷新使用此方法選擇的行
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths
選擇單元格時為什么要使單元出隊。 編寫以下代碼
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
NSLog(@"Select");
cell *selectedCell= (cell *)[collectionView cellForItemAtIndexPath:indexPath];
[selectedCell.mySelect setImage:(selectedCell.selected) ? [UIImage imageNamed:@"select.png"]: nil];
[collectionView reloadItemsAtIndexPaths:indexPath];
}
我認為您應該為此任務重寫自定義UICollectionViewCell
子類中的selected/highlighted
屬性,而不是使用reload。
cell *selectedCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
此代碼將創建一個NEW單元! 您的viewController不包含該單元格!
您應該使用正確的方法來獲取選定的單元格:
[yourContentView cellForItemAtIndexPath:indexPath];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.