Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide selection on UITableview?

Tags:

iphone

i want to disable click on particular Cell.it means, i want not to show highlight color(selection indication) when we touch on particular cell? any help please?

like image 637
senthilM Avatar asked Nov 25 '09 11:11

senthilM


2 Answers

Either use cell.selectionStyle = UITableViewCellSelectionStyleNone; or return false or return null in the delegate method tableView:willSelectRowAtIndexPath.

like image 80
luvieere Avatar answered Sep 20 '22 01:09

luvieere


Swift:

If you're using a custom cell:

class YourCustomCell: UITableViewCell {
    override func awakeFromNib() {
        setup()
    }

    init() {
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    fileprivate func setup() {
        self.selectionStyle = .none
    }
}

If you're not using a custom cell:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifierID")
    cell.selectionStyle = .none
    return cell
}

Bonus: If you want to hide the cell selection and also don't want to call func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {...} then only set cell.isUserInteractionEnabled = false. However, this is not a good practice.

like image 20
Hemang Avatar answered Sep 20 '22 01:09

Hemang