ios 在 UICollectionView 中以编程方式选择项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13177201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:03:19  来源:igfitidea点击:

Select items programmatically in UICollectionView

iosobjective-cuicollectionview

提问by Ali

I have a UICollectionViewController:

我有一个UICollectionViewController

- (NSInteger)collectionView:(UICollectionView *)collectionView 
     numberOfItemsInSection:(NSInteger)section {
    return [self.pageTastes count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     CellTasteCollectionView *cell = 
       [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" 
                                                 forIndexPath:indexPath];
     Taste *taste = [self.pageTastes objectAtIndex:indexPath.item];       
     [[cell imageView] setImage:taste.image];
     [cell setObjectId:taste.objectId];    
     return cell;
}

It works. I have this in viewDidLoad, allowing the user to choose multiple items:

有用。我有这个viewDidLoad,允许用户选择多个项目:

[self.collectionView setAllowsMultipleSelection:YES];

What I want to have, is that the first time the CollectionView loads, some items get selected programmatically, based on their objectIdin CellTasteCollectionView.

我想要的是,第一次加载 CollectionView 时,某些项目会根据它们的objectIdin 以编程方式被选中CellTasteCollectionView

Here's how I'm doing this:

这是我的做法:

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

    Taste *taste = [self.pageTastes objectAtIndex:indexPath.item];
    printf("%s\n", [taste.objectId UTF8String]);
}

It's called when the user clicks on the item -- this is not what I want: I want the item to be selected automatically when UICollectionViewloads.

当用户单击项目时调用它——这不是我想要的:我希望在UICollectionView加载时自动选择项目。

How do I do this?

我该怎么做呢?

回答by Masa

I think you are missing this method from the UICollectionView Class Reference:

我认为您在UICollectionView 类参考中缺少此方法:

- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath 
                     animated:(BOOL)animated 
               scrollPosition:(UICollectionViewScrollPosition)scrollPosition

You can use this method multiple times if you want multiple selections.

如果您想要多个选择,您可以多次使用此方法。

回答by Baig

didSelectItemAtis not called if you call selectItemprogrammatically. You should call the method manually after it.

didSelectItemAt如果您以selectItem编程方式调用,则不会调用。您应该在它之后手动调用该方法。

self.collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: .bottom)
self.collectionView(self.collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))

回答by Michael Yang

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.districtTableview selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self tableView:weakSelf.districtTableview didSelectRowAtIndexPath:indexPath];

I used the above method in tableview, and it worked.

我在 tableview 中使用了上述方法,并且有效。

回答by akaDuality

For right behaviour call 4 function in a row:

对于正确的行为,连续调用 4 个函数:

// Deselect
self.collection.deselectItem(at: previousPath, animated: true)
self.collectionView(self.collection, didDeselectItemAt: previousPath)

// Select
self.collection.selectItem(at: path, animated: true, scrollPosition: .centeredVertically)
self.collectionView(self.collection, didSelectItemAt: path)