Disable selection of a single UITableViewCell

前端 未结 9 2084
迷失自我
迷失自我 2021-01-31 08:32

How do you disable selecting only a single cell in a UITableView? I have several, and I only want the last to be disabled.

9条回答
  •  盖世英雄少女心
    2021-01-31 09:14

    If anyone wondering how to achieve this in swift then here is my code. I am using Xcode 7 and tested using iPad Retina(iOS 9).

    cell.selectionStyle = UITableViewCellSelectionStyle .None
    cell.userInteractionEnabled = false
    

    Try to place this two line code whether you want. In my case I have used this in this method for displaying cells.

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
    

    Remember this two line code will block any kind of selection or interaction to your cells but you can only use the first line individually if you want. That is...

    cell.selectionStyle = UITableViewCellSelectionStyle .None
    

    Only this line will block the selection to your cells.

    However the second line will make the cell "Read-Only". That is..

    cell.userInteractionEnabled = false
    

    Thanks

    Hope this helped.

提交回复
热议问题