sus*_*t86 29 selection uicollectionview
我在CollectionView中有多个项目,但只有少数项目可以选择.我正在使用委托方法处理这个:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
如果选择了可选项,则会出现问题,并且在下一步中,如果shouldSelectItemAtIndexPath返回NO,则拒绝选择不可选项,否则将取消选择所选项.
我也试过用
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
但这是同样的问题.
这是UICollectionView正确的期望行为吗?如果是,如果选择了一个不可选择的项目,我怎样才能避免取消选中我上次选择的项目?
sus*_*t86 13
我必须纠正我的假设:最后的选择不会被取消选择!
为了在选择时改变单元格的外观,我已经覆盖了UICollectionViewCell的setSelected访问器.选择不可选择的项目时,会多次调用最后一个选定单元格的访问者setSelected.首先是状态NO,然后是状态YES,最后是NO.最后一个状态NO导致我的单元格将其外观设置为非选定单元格的外观.
我不知道这种奇怪行为的原因也无法解决.
我的解决方法是直接在ViewController中更改所选单元格的外观.
将选定的外观设置为:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
删除所选外观:
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
检查当前单元格是否已选中并按预期更改外观.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15694 次 |
| 最近记录: |