【问题标题】:Changing the state of the CollectionView Cell更改 CollectionView 单元的状态
【发布时间】:2015-03-17 08:03:13
【问题描述】:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    //NSLog(@"enter category cell");

    CategoryViewCell* cell = (CategoryViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    [cell.imgCat setImage:[UIImage imageNamed:[categoryImages objectAtIndex:indexPath.row]]];
    [cell.labelCatName setText:[[NSString stringWithFormat:@"%@", [catName objectAtIndex:indexPath.row]] capitalizedString]];
    if([categories[[NSString stringWithFormat:@"%d",(int)indexPath.row]] isEqual:@YES]) {
        //NSLog(@"set border");

        cell.layer.borderColor = [UIColor redColor].CGColor;
        cell.layer.borderWidth = 3;

    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryViewCell* cell = (CategoryViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    cell.layer.borderWidth = 3;
    cell.layer.borderColor = [UIColor redColor].CGColor;

    //NSLog(@"%i", (int)indexPath.item);
    categories[[NSString stringWithFormat:@"%i", (int)indexPath.item]] = @YES;



}

-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    CategoryViewCell* cell = (CategoryViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    cell.layer.borderColor = [UIColor clearColor].CGColor;

    categories[[NSString stringWithFormat:@"%i", (int)indexPath.item]] = @NO;
}

问题: 上面的代码将向您展示一个 collectionView,其中包含默认选中的单元格。但是,这些选定单元格的状态并未被选中。所以我必须点击两次才能取消选择它们,因为第一次点击是选择它们,第二次点击是取消选择它们。

我尝试为单元格设置选择,但它也不起作用。每当用户选择一个单元格时,该单元格将有一个红色边框,当用户取消选择该单元格时,该单元格将有一个红色边框。

我试过了:

cell.selected = YES;

但这会永久地给 collectionView Cell 一个红色边框。

并且在 cellForItemAtIndexPath 方法中添加它仍然不能解决问题。

[self collectionView:collectionView didSelectItemAtIndexPath:indexPath];

CategoryViewCell.m

-(void) setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected];
    //NSLog(@"Pass Select Animated");
    if (selected) {
        self.flagHighlight = NO;
        self.selected = NO;
        self.layer.borderColor = [UIColor clearColor].CGColor;
        self.layer.borderWidth = 3;
    } else {
        self.flagHighlight = YES;
        self.selected = YES;
        self.layer.borderColor = [UIColor redColor].CGColor;
        self.layer.borderWidth = 3;
    }
}

当视图以编程方式加载时,如何预先选择一个单元格? 或者甚至更好地改变被选中的单元格的状态。

提前致谢。

【问题讨论】:

  • 由于您使用自定义UICollectionViewCell,首先,我会在您设置边框颜色的位置覆盖setSelected:animated(或类似的东西)。然后在collectionView: cellForItemAtIndexPath: 中执行cell.selected = YES
  • 我是按照你的方法走的,不过也有不清楚的地方,我在Cell.m中设置了self.layer.borderColor和width。但它似乎并没有添加边框。你有任何源代码我可以看看吗?感谢您的留言。
  • 你能说明你在 CategoryViewCell.m 中添加了什么方法吗?
  • 当然,我添加了上面的代码。在 cellForItemAtIndexPath 中,我添加了 [cell setSelected:YES animated:NO];.

标签: ios objective-c uicollectionview


【解决方案1】:

最终回答了我自己的问题。

所以我使用[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

在 cellForItemAtIndexPath 中。

很好地服务于目的。

【讨论】:

    猜你喜欢
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多