iOS:集合视图,selectItemAtIndexPath以编程方式不起作用

时间:2013-12-13 10:07:23

标签: ios uicollectionview

我不知道为什么这不起作用。 indexOfIcon是正确的,部分是正确的(用NSLog检查)如果我选择一个一切都是正确的。但这条线没有做什么......为什么?如果选中它应该有一个蓝色边框。这在“手动”但不是代码的情况下很有效。

- (void)viewWillAppear:(BOOL)animated
{
    NSUInteger indexOfIcon;
    if(self.mainCategory.icon){
        indexOfIcon = [self.icons indexOfObject: self.mainCategory.icon];
    } else {
        indexOfIcon = 0;
    }
    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:indexOfIcon inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionBottom];
}

2 个答案:

答案 0 :(得分:9)

添加

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.selected = YES;

将选择单元格。

命令[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:indexOfIcon inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionBottom];告诉collectionView,单元格处于选中状态,但不设置单元格的状态。

答案 1 :(得分:0)

经过多年的选择似乎工作正常(即使在viewDidLoad:)

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[_collection selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionLeft];

成功触发了单元格-setSelected:

相关问题