didDeselectItemAtIndexPath中单元格的图像不会更改

时间:2014-03-03 09:12:31

标签: ios uicollectionview

我尝试更改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;    }

}

当我点击任何单元格时,值选择会更改,但视图对象不会刷新。

4 个答案:

答案 0 :(得分:0)

您需要使用此方法从didSelectItemAtindexPath方法

刷新您选择的行

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

答案 1 :(得分:0)

为什么在选择单元格时出列Cell。 写下面的代码

 - (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];
 }

答案 2 :(得分:0)

我认为您应该在自定义selected/highlighted子类中覆盖此UICollectionViewCell属性,而不是使用重新加载。

答案 3 :(得分:0)

cell *selectedCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

此代码将创建一个新单元格!你的viewController不包含这个单元格!

您应该使用正确的方法获取所选单元格:

[yourContentView cellForItemAtIndexPath:indexPath];