关于 UICollectionViewCell 的一些误点

如果直接使用 UICollectionViewCell 的自带属性 selected

来自定义一些样式,如:

- (void)setSelected:(BOOL)selected {

[super setSelected:selected];

[self setNeedsDisplay];

}

,那么当你reloadData 且在

cellForItemAtIndexPath 方法中给其 selected 属性设置YES 后,无论如何你是不能触发下面两个取消选中的代理方法:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; // called when the user taps on an already-selected item in multi-select mode

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;

也就无法达到取消选中的功能。

有两种解决方法:

1.

在 cellForItemAtIndexPath 方法中给其 selected 属性设置YES 后,再使用 selectItemAtIndexPath: animated: scrollPoisition: 方法将其设置为选中 Item:

这样你再点击这个 Item 的时候就可以触发

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; // called when the user taps on an already-selected item in multi-select mode

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;

两个发法。

2. 使用自定义的选中状态属性来代替 UICollectionViewCell 自带的 selected 属性:

再在此属性的 setter 方法中自定义样式:

那么即使你在cellForRow 中给 selectedState 赋值也不影响取消选中的代理方法的使用。然后还将出现的另一个陷阱就是:

当你(转屏后)reloadData 以后,选中的 selected Item 将重新返回到 ”非选中Item“状态,当你点击的时候并不会触发

shouldDeselectItemAtIndexPath 方法,而是触发 shouldSelectItemAtIndexPath 方法,因此你还需在 shouldSelectItemAtIndexPath 方法中再做一次处理:

就是使用 deselectItemAtIndexPath: animated: 将其直接设置为你想取消选中的状态,并同时处理cell 非选中 状态下的样式。

时间: 2024-08-01 11:17:49

关于 UICollectionViewCell 的一些误点的相关文章

[IOS]swift自定义uicollectionviewcell

刚刚接触swift以及ios,不是很理解有的逻辑,导致某些问题.这里分享一下swift自定义uicollectionviewcell 首先我的viewcontroller不是直接继承uicollectionviewcontroller,而是添加的uicollectionview到我的storyboard, 然后再新建一个swift的文件,让这个swift继承uicollectionviewcell import Foundation class SVGCell :UICollectionView

委托错误点

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 委托练习 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 string s = Console.ReadLine(); 13 if (s == "1") 14 { 15 SomeDeleg

UICollectionViewCell定制Button

UICollectionViewCell定制Button 效果 特点 1.能够动态设置每行显示的按钮的个数,以及控件的摆放格式 2.实现单选或者多选的功能,实现点击事件 3.自定制按钮的显示样式 用法 1.下载源码后,将文件中的GridCollectionView.h/.m文件,  CustomCollectionViewCell.h/.m文件,  TypeCellClass.h/.m文件导入工程中. 2.注意:你所创建的cell要继承CustomCollectionViewCell.然后你的c

Xcode升级误点Skip Bundle解决办法!!!

交代一下背景,我电脑上是从AppStore下载的Xcode7.2,后来从网上下载了Xcode7.3安装包,然后安装7.3,电脑上同时存在7.2版本和7.3版本,但是在安装7.3的时候误点了Skip Bundle,然后工程就有一点小问题,下面是解决办法: 第一步:“Finder”--->"应用程序"--->"鼠标右键点击7.3版本Xcode"----->“显示包内容”. 第二步:“Contents”---->"info.plist&q

UICollectionViewCell「居左显示」

UICollectionViewCell「居左显示」 准备: 1.UICollectionView Left Aligned Layout 一款UICollectionView居左显示的约束点击下载_UICollectionView Left Aligned Layout UICollectionView Left Aligned Layout 工程目录: 工程目录 自定义UICollectionViewCell CollectionViewCell.h 创建UILabel属性,用来传值 #im

Keymob为加速广告转化 推广告误点过滤功能

Keymob作为一个专业的移动广告平台,为加速广告转化,推出新的功能:广告误点过滤功能. 随着移动互联网时代的到来,人们在手机上网中,受限于屏幕和键盘的大小,进行手指按下和滑动操作的时候,很容易出现按错键或按错按钮的现象.业内把这种现象称之为 “胖手指”(fat finger)效应,且多以“胖手指综合征”(fat-finger syndrome)的词组形式出现. 胖手指现象同样存在于的商业广告中,由于用户并不是主动意愿去点击广告,这些误点对应转化效率非常低,严重伤害到广告主ROI,因此需要将这部

在StoryBoard对UICollectionViewCell 进行Autolayout是遇到的Xcode6.01的BUG

使用Sb对UICollectionViewCell 的内容进行Autolayout约束时候,发现了一个Xcode6.01的BUG,就是你对UICollectionCell约束完了之后,在模拟器上现实的Label是居中,但是真机显示的确实不是居中,后来Google了一下,发现了问题,这是因为使用ios8 SDk编译出来的项目运行在iOS 7引起ContentView大小没有变导致的,解决办法之一:就是在你的定义的UICollectionViewCell 的子类重LayoutSubviews方法,在

关于N阶常系数差分方程和微分方程的对比易误点(d ' y(t)/dt 对应 y[n - 1] ? )

Linear Constant-coefficient difference equations 这里有个容易"让人困惑"的地方,仔细观察两个方程,加上这里书上说了一句"1.109式(图中上面的公式),对应的离散时间方程是下面的方程" 发现会有一种感觉,"既然是对应的,那么这里d ' y(t)/dt 岂不是对应 y[n - 1]  (当k = 1时)" 正是这样,我就陷入了深深的困惑... 会觉得"怎么连续领域内的一阶导数等于离散领域内

通过Xib创建 UICollectionView 和自定义UICollectionViewCell

1.在控制器的viewDidLoad方法中添加代码 CGFloat itemWidth = (kScreenW - kSpacingW * 3) / 2; NSLog(@"itemWidth == %f",itemWidth); CGFloat itemHeight = itemWidth * 0.75 + 71; NSLog(@"itemWidth == %f",itemHeight); UICollectionViewFlowLayout*layout = [[