当您点击一个单元格时,该行被选中并突出显示。现在我想做的是禁用突出显示但允许选择。有没有办法解决它。有一个问题可以回答这个问题,但它会禁用选择和突出显示。
cell.selectionStyle = UITableViewCellSelectionStyleNone;
将此添加到您的 cellForRowAtIndexPath
方法中
您可以从 Storyboard 中将单元格的选择样式设置为“无”:
https://i.stack.imgur.com/HX9qI.png
或从代码:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
对于斯威夫特 3:
cell.selectionStyle = UITableViewCellSelectionStyle.none
对于 Swift 4 及更高版本:
cell.selectionStyle = .none
将 UITableViewCell
的 selectedBackgroundView
颜色更改为透明。
let clearView = UIView()
clearView.backgroundColor = UIColor.clearColor() // Whatever color you like
UITableViewCell.appearance().selectedBackgroundView = clearView
或为特定单元格设置:
cell.backgroundView = clearView
cell.selectionStyle = UITableViewCellSelectionStyleNone
有时会破坏 Apple 动画逻辑内部的某些内容。
UITableViewCell.appearance().selectionStyle = .None
awakeFromNib()
函数中很有效
尝试将单元格选择样式设置为无 -
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
这将解决您的问题
对于 Swift 3.0:
cell.selectionStyle = .none
对于斯威夫特:
UITableViewCell.selectionStyle = UITableViewCellSelectionStyle.None;
或者当您快速子类化一个单元格时:
class CustomCell : UITableViewCell {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
selectionStyle = .None
}
}
在迅速 3
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
要添加自定义颜色,请使用以下代码。并使其透明使用 alpha: 0.0
cell.selectedBackgroundView = UIView(frame: CGRect.zero)
cell.selectedBackgroundView?.backgroundColor = UIColor(red:0.27, green:0.71, blue:0.73, alpha:1.0)
如果您使用自定义颜色并希望给它圆角外观,请使用:
cell.layer.cornerRadius = 8
此外,使用它以获得更好的动画和感觉
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
对于那些在 Swift 3 中寻找编程方式的人
cell.selectionStyle = UITableViewCellSelectionStyle.none
https://i.stack.imgur.com/nYMo0.png
对于对象:
[单元格 setSelectionStyle:UITableViewCellSelectionStyleNone];
- (void)viewDidLoad {
[super viewDidLoad];
_tableView.allowsSelection = YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.. .. .. ..
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
. . . . ..
}
对于 Swift 5,最好的方法是:
cell.selectionStyle = .none
这是 swift 3 的解决方案,即使在编辑模式下也可以使用
cell.selectionStyle = .gray
cell.selectedBackgroundView = {
let colorView = UIView()
colorView.backgroundColor = UIColor.black.withAlphaComponent(0.0)
//change the alpha value or color to match with you UI/UX
return colorView
}()
不定期副业成功案例分享