epo*_*gee 40 cocoa-touch objective-c uitableview subclassing
我当前项目的UITableViewCell行为令我感到困惑.我有一个相当简单的UITableViewCell子类.它为基本视图添加了一些额外的元素(通过[self.contentView addSubview:...]并设置元素上的背景颜色,使它们看起来像黑色和灰色矩形框.
因为整个表格的背景具有这种具体的纹理图像,所以每个单元格的背景都需要透明,即使在选中时也是如此,但在这种情况下它应该变暗一点.我已经设置了一个自定义的半透明选定背景来实现这个效果:
UIView *background = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
background.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
background.opaque = NO;
[self setSelectedBackgroundView:background];
Run Code Online (Sandbox Code Playgroud)
虽然这样会产生正确的背景效果,但当我选择单元格时会出现奇怪的副作用; 所有其他背景在某种程度上turnt 关闭.这是一个截图.底部单元看起来应该并且未被选中.顶部单元格被选中,但它应该显示黑色和灰色的矩形区域,但它们已经消失了!

谁知道这里发生了什么,更重要的是:我怎么能纠正这个?
NSD*_*yer 53
发生的事情是TableViewCell中的每个子视图都将接收setSelected和setHighlighted方法.setSelected方法将删除背景颜色,但如果将其设置为选定状态,则会更正.
例如,如果这些是在自定义单元格中添加为子视图的UILabel,则可以将其添加到TableViewCell实现代码的setSelected方法中:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
self.textLabel.backgroundColor = [UIColor blackColor];
}
Run Code Online (Sandbox Code Playgroud)
self.textLabel将是上图中显示的那些标签
我不确定您添加所选视图的位置,我通常会在setSelected方法中添加它.
或者,您可以继承UILabel并覆盖setHighlighted方法,如下所示:
-(void)setHighlighted:(BOOL)highlighted
{
[self setBackgroundColor:[UIColor blackColor]];
}
Run Code Online (Sandbox Code Playgroud)
Oli*_*ain 41
如果您不知道发生了什么,单元格突出显示过程看起来很复杂且令人困惑.我彻底困惑,做了一些广泛的实验.以下是我的调查结果可能有助于某人的注释(如果有人有任何要添加或反驳的话请注释,我会尽力确认和更新)
在正常的"未选择"状态
selectedBackgroundView隐藏backgroundView是可见的(因此,如果您的contentView是透明的,您会看到backgroundView或(如果您尚未定义,backgroundView您将看到UITableView自身的背景颜色)选择一个单元格,随后出现以下任何动画:
backgroundColor清除(或设置为透明),标签等文本颜色更改为所选颜色selectedBackgroundView变得可见(这个观点始终是电池的全尺寸(自定义帧被忽略,使用一个子视图如果需要).另外还要注意backgroundColor的subViews是,不显示出于某种原因,也许他们正在设置透明像contentView) .如果你没有定义一个selectedBackgroundView然后Cocoa会创建/插入蓝色(或灰色)渐变背景并为你显示这个)backgroundView不变取消选择单元格后,将开始删除突出显示的动画:
selectedBackgroundViewalpha属性是从1.0(完全不透明)到0.0(完全透明)的动画.backgroundView是再次改变(让动画看起来之间的交叉淡入淡出selectedBackgroundView和backgroundView)contentView重新绘制"未选中"状态并且其子视图backgroundColor再次可见(这可能会导致您的动画看起来很糟糕,因此建议您不要使用UIView.backgroundColor它contentView)结论:
如果你需要一个backgroundColor坚持高亮动画,不要使用你可以尝试的backgroundColor属性UIView(可能是with-in tableview:cellForRowAtIndexPath:):
具有背景颜色的CALayer:
UIColor *bgColor = [UIColor greenColor];
CALayer* layer = [CALayer layer];
layer.frame = viewThatRequiresBGColor.bounds;
layer.backgroundColor = bgColor.CGColor;
[cell.viewThatRequiresBGColor.layer addSublayer:layer];
Run Code Online (Sandbox Code Playgroud)
或者CAGradientLayer:
UIColor *startColor = [UIColor redColor];
UIColor *endColor = [UIColor purpleColor];
CAGradientLayer* gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = viewThatRequiresBGColor.bounds;
gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
gradientLayer.locations = @[[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:1]];
[cell.viewThatRequiresBGColor.layer addSublayer:gradientLayer];
Run Code Online (Sandbox Code Playgroud)
我还使用了CALayer.border技术来提供自定义UITableView分隔符:
// We have to use the borderColor/Width as opposed to just setting the
// backgroundColor else the view becomes transparent and disappears during
// the cell's selected/highlighted animation
UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
separatorView.layer.borderColor = [UIColor redColor].CGColor;
separatorView.layer.borderWidth = 1.0;
[cell.contentView addSubview:separatorView];
Run Code Online (Sandbox Code Playgroud)
Chr*_*lay 17
当您开始拖动UITableViewCell时,它会setBackgroundColor:使用0-alpha颜色调用其子视图.我通过继承UIView并覆盖setBackgroundColor:忽略具有0-alpha颜色的请求来解决这个问题.这感觉哈克,但它比清洁任何的的其他 解决方案,我已经遇到.
@implementation NonDisappearingView
-(void)setBackgroundColor:(UIColor *)backgroundColor {
CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
if (alpha != 0) {
[super setBackgroundColor:backgroundColor];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
然后,我将一个添加NonDisappearingView到我的单元格并添加其他子视图:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
UIView *background = [cell viewWithTag:backgroundTag];
if (background == nil) {
background = [[NonDisappearingView alloc] initWithFrame:backgroundFrame];
background.tag = backgroundTag;
background.backgroundColor = backgroundColor;
[cell addSubview:background];
}
// add other views as subviews of background
...
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以使cell.contentView成为实例NonDisappearingView.
我的解决方案是backgroundColor在超级调用之后保存并恢复它.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
UIColor *bgColor = self.textLabel.backgroundColor;
[super setSelected:selected animated:animated];
self.textLabel.backgroundColor = bgColor;
}
Run Code Online (Sandbox Code Playgroud)
你还需要做同样的事情-setHighlighted:animated:.
| 归档时间: |
|
| 查看次数: |
19599 次 |
| 最近记录: |