【问题标题】:disable the uitableview highlighting but allow the selection of individual cells禁用 uitableview 突出显示但允许选择单个单元格
【发布时间】:2023-03-12 11:34:02
【问题描述】:

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

【问题讨论】:

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

标签: ios objective-c uitableview


【解决方案1】:

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

或者来自代码:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

对于 Swift 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none

对于 Swift 4 及更高版本:

cell.selectionStyle = .none

【讨论】:

  • 当我选择“无”时,我失去了高亮状态的工作能力。我可以在不丢失该状态的情况下将背景设为无/白色吗?
  • 这会用到哪个函数?我猜是“cellForRowAt”?
  • 你应该在单元格而不是表格视图中进行操作
【解决方案2】:

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() 函数中会很有效
  • 此选项使表格视图单元格分隔线不可见。
【解决方案3】:

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

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

这会解决你的问题

【讨论】:

    【解决方案4】:

    对于 Swift 3.0

    cell.selectionStyle = .none
    

    【讨论】:

      【解决方案5】:

      对于 Swift:

      UITableViewCell.selectionStyle = UITableViewCellSelectionStyle.None;

      或者当你在 swift 中子类化一个单元格时:

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

      【讨论】:

        【解决方案6】:

        在 Swift 3 中

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            【解决方案8】:

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

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            【讨论】:

              【解决方案9】:

              您可以在故事板中设置单元格本身的选择属性

              【讨论】:

                【解决方案10】:

                对于对象:

                [单元格设置SelectionStyle:UITableViewCellSelectionStyleNone];

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

                【讨论】:

                  【解决方案11】:

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

                  cell.selectionStyle = .none
                  

                  【讨论】:

                    【解决方案12】:

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

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-09-19
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2014-12-07
                      • 2013-11-07
                      相关资源
                      最近更新 更多