Sim*_*iwi 80 objective-c click selection uitableview ios
当我点击我的时UITableViewCell
,当我点击单元格时,背景部分(我的背景图像未覆盖的区域)变为蓝色.此外,UILabel
当单击时,单元格上的所有s都变成白色,这就是我想要的.
然而,当我点击它时,我不想要的是蓝色背景,但如果我这样做selectionstylenone
,那么我将丢失UILabel
单元格中s 的突出显示颜色.
那么有什么方法可以在单击单元格时去除蓝色背景但是保留UILabel
s 的突出显示颜色?
jon*_*oll 171
您可以按如下方式执行此操作.将表格单元格的选择样式设置为UITableViewCellSelectionStyleNone
.这将删除突出显示的蓝色背景.然后,要使文本标签突出显示以您希望的方式工作,而不是使用默认的UITableViewCell类,创建子类UITableViewCell
并setHighlighted:animated
使用您自己的实现覆盖默认实现,根据突出显示的状态将标签颜色设置为您想要的.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted) {
self.textLabel.textColor = [UIColor whiteColor];
} else {
self.textLabel.textColor = [UIColor blackColor];
}
}
Run Code Online (Sandbox Code Playgroud)
Alf*_*lfa 72
如果在iOS7之前工作,请将您的单元格选择样式设为无
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)
另外,请留下 UITableViewCellSelectionStyleDefault
然后:
UIView *selectedView = [[UIView alloc]init];
selectedView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedView;
Run Code Online (Sandbox Code Playgroud)
此代码将正常工作
Sca*_*car 16
将选择样式设置为none后,可以使用以下委托方法:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
像这样在这里实现你的代码
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[cell.lbls setTextColor:[UIColor whiteColor]];
return indexPath;
}
Run Code Online (Sandbox Code Playgroud)
Mru*_*nal 13
在cellForRowAtIndexPath中使用以下代码:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myLabel setHighlightedTextColor: [UIColor whiteColor]]; // for all your labels
Run Code Online (Sandbox Code Playgroud)
希望这对你有用.
享受编码:)
who*_*9vy 11
要完成这项工作,您必须将选择样式设置为UITableViewCellSelectionStyleNone
,然后您应该覆盖该方法setSelected:animated:
以获得所需的结果.当你看到蓝色(或灰色)选择时,iOS的自动选择机制也是如此.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
} else {
self.textLabel.textColor = [UIColor blackColor];
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以通过其他方式对其进行自定义,例如通过更改UITableViewCell背景等.
覆盖UITableViewCell
.
override func setHighlighted(highlighted: Bool, animated: Bool) { }
override func setSelected(selected: Bool, animated: Bool) { }
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
91442 次 |
最近记录: |