hpi*_*que 34 ios ios6 uicollectionview uicollectionviewcell
根据Collection View编程指南,应该处理单元格高光的视觉状态UICollectionViewDelegate
.像这样:
- (void)collectionView:(PSUICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
[cell highlight];
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
[cell unhighlight];
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这种方法的是它为委托添加了非常特定于单元格的逻辑.事实上,UICollectionViewCell
通过highlighted
财产独立管理其突出显示的状态.
那么,压倒一切setHighlighted:
不是一个更清洁的解决方案吗?
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
if (highlighted) {
[self highlight];
} else {
[self unhighlight];
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法有没有缺点而不是委托方法?
A-L*_*ive 51
正如文档所述,您可以依赖于在highlighted
突出显示单元格时更改属性.例如,以下代码将在突出显示时使单元格变为红色(但不是其子视图):
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
if (self.highlighted) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1, 0, 0, 1);
CGContextFillRect(context, self.bounds);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你添加这样的东西,背景将变成紫色(红色+不透明的蓝色):
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = nil;
}
Run Code Online (Sandbox Code Playgroud)
所以你可以将两者结合使用(不一定都改变细胞外观).不同的是,您也可以使用委托方法indexPath
.它可能用于创建多选(您将使用此方法与选择委托方法一起),在单元格突出显示时显示一些预览,以显示其他视图的一些动画...这个委托有相当多的设备在我看来的方法.
作为结论,我会让单元格外观由单元格本身处理,并使用委托方法让控制器在同一时间制作一些很酷的东西.
biz*_*z84 13
下面概述了两种可能的方法.
细胞子类化
更清洁的方法,如果已经从子类化UICollectionViewCell
.
class CollectionViewCell: UICollectionViewCell {
override var highlighted: Bool {
didSet {
self.contentView.backgroundColor = highlighted ? UIColor(white: 217.0/255.0, alpha: 1.0) : nil
}
}
}
Run Code Online (Sandbox Code Playgroud)
UICollectionViewDelegate
不太干净,需要集合视图委托了解单元格的表示逻辑.
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
cell.contentView.backgroundColor = UIColor(white: 217.0/255.0, alpha: 1.0) // Apple default cell highlight color
}
}
func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
cell.contentView.backgroundColor = nil
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,它UICollectionViewCell
有一个selectedBackgroundView
属性.默认情况下,它是零.只需为此属性创建一个视图,它将在用户触摸单元格时显示.
override func awakeFromNib() {
super.awakeFromNib()
let view = UIView(frame: contentView.bounds)
view.isUserInteractionEnabled = false
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.backgroundColor = UIColor(white: 0.94, alpha: 1.0)
selectedBackgroundView = view
}
Run Code Online (Sandbox Code Playgroud)
嗯......因为所有这些方法都是正确的.我找到了对我来说最简单的方式.只需覆盖setSelected:方法(例如更改背景颜色):
-(void)setSelected:(BOOL)selected{
self.backgroundColor = selected?[UIColor greenColor]:[UIColor grayColor];
[super setSelected:selected];
}
Run Code Online (Sandbox Code Playgroud)
...它"开箱即用"(即使使用collectionView.allowsMultipleSelection)
足以突出显示单元格(Swift 4)
class MyCollectionViewCell: UICollectionViewCell {
...
override var isHighlighted: Bool {
didSet {
if isHighlighted {
self.contentView.alpha = 0.6
}
else {
self.contentView.alpha = 1.0
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
34309 次 |
最近记录: |