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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30233468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:58:20  来源:igfitidea点击:

disable the uitableview highlighting but allow the selection of individual cells

iosobjective-cuitableview

提问by soldiershin

when you tap on a cell the row gets selected and highlighted.Now what i want to do is disable the highlighting but allow the selection.Is there a way around it.There is question that answers this but it disables both the selection and highlighting.

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

回答by József Vesza

You can just set the cell's selection style to "None" from Storyboard:

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

See here

看这里

Or from code:

或从代码:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

For Swift 3:

对于 Swift 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none

For Swift 4 & above:

对于 Swift 4 及以上版本:

cell.selectionStyle = .none

回答by tounaobun

Change UITableViewCell's selectedBackgroundViewcolor to transparent.

UITableViewCellselectedBackgroundView颜色更改为透明。

    let clearView = UIView()
    clearView.backgroundColor = UIColor.clearColor() // Whatever color you like
    UITableViewCell.appearance().selectedBackgroundView = clearView

or to set for a specific cell:

或为特定单元格设置:

cell.backgroundView = clearView

cell.backgroundView = clearView

回答by Viper

Try setting cell selection style to None -

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

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

This will solve your problem

这将解决您的问题

回答by Eliko

For Swift 3.0:

对于Swift 3.0

cell.selectionStyle = .none

回答by Melvin

For Swift:

对于斯威夫特:

UITableViewCell.selectionStyle = UITableViewCellSelectionStyle.None;

UITableViewCell.selectionStyle = UITableViewCellSelectionStyle.None;

Or when you subclass a cell in swift:

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

class CustomCell : UITableViewCell {

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

}

回答by Sai kumar Reddy

in swift 3

在迅速 3

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

回答by M.E

You can set the selection property of the cell itself in the storyboard enter image description here

您可以在故事板中设置单元格本身的选择属性 在此处输入图片说明

回答by Danoram

For those looking for a programmatic way in Swift 3

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

cell.selectionStyle = UITableViewCellSelectionStyle.none

cell.selectionStyle = UITableViewCellSelectionStyle.none

回答by Lakhdeep Singh

For Objc:

对于对象:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

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

回答by ramzesenok

For Swift 5the best way is:

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

cell.selectionStyle = .none