ChatGPT解决这个技术问题 Extra ChatGPT

禁用 uitableview 突出显示但允许选择单个单元格

当您点击一个单元格时,该行被选中并突出显示。现在我想做的是禁用突出显示但允许选择。有没有办法解决它。有一个问题可以回答这个问题,但它会禁用选择和突出显示。

cell.selectionStyle = UITableViewCellSelectionStyleNone; 将此添加到您的 cellForRowAtIndexPath 方法中

A
Antoine Rucquoy

您可以从 Storyboard 中将单元格的选择样式设置为“无”:

https://i.stack.imgur.com/HX9qI.png

或从代码:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

对于斯威夫特 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none

对于 Swift 4 及更高版本:

cell.selectionStyle = .none

当我选择“无”时,我失去了突出显示状态的工作能力。我可以在不丢失该状态的情况下将背景设为无/白色吗?
这将用于哪个功能?我猜是“cellForRowAt”?
您应该在单元格而不是表格视图中执行此操作
q
quantumpotato

UITableViewCellselectedBackgroundView 颜色更改为透明。

    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() 函数中很有效
此选项使表格视图单元格分隔线不可见。
V
Viper

尝试将单元格选择样式设置为无 -

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

这将解决您的问题


E
Eliko

对于 Swift 3.0:

cell.selectionStyle = .none

n
n13

对于斯威夫特:

UITableViewCell.selectionStyle = UITableViewCellSelectionStyle.None;

或者当您快速子类化一个单元格时:

class CustomCell : UITableViewCell {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        selectionStyle = .None
    }

}

S
Sai kumar Reddy

在迅速 3

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
}

P
Prateekro

要添加自定义颜色,请使用以下代码。并使其透明使用 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)
}

D
Danoram

对于那些在 Swift 3 中寻找编程方式的人

cell.selectionStyle = UITableViewCellSelectionStyle.none


t
thor

https://i.stack.imgur.com/nYMo0.png


L
Lakhdeep Singh

对于对象:

[单元格 setSelectionStyle:UITableViewCellSelectionStyleNone];

- (void)viewDidLoad {
  [super viewDidLoad];
  _tableView.allowsSelection = YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .. .. .. .. 
   [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
   . . . . .. 
}

r
ramzesenok

对于 Swift 5,最好的方法是:

cell.selectionStyle = .none

D
DukeX

这是 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
}()