James Joshua Street
James Joshua Street

Reputation: 3409

how to disable selection on inset of UITableCell in swift?

I created fake space between UITableViewCell by insetting the content.

contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0))

However, the UITableCell still accepts selection events based when selecting between the cells because even the non-inset 8 pixels is still contained within the UITableViewCell.

Is there a proper way of doing this so that selection does not occur?

I feel like this is a hack, so I am wondering what the more normal way of doing things is. I kind of would expect a property on the tableview itself to set the inset from interface builder

Upvotes: 1

Views: 181

Answers (1)

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

You can always override hitTest or pointInside in your tableView cell. You have mentioned that you are setting the contentView.frame assuming that its updating fine, you can simply use one of the below implementation

class SOTableViewCell: UITableViewCell {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if contentView.frame.contains(point) {
            return super.hitTest(point, with: event) /*return self*/
        }
        return nil
    }
}

Or

class SOTableViewCell: UITableViewCell {
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        return contentView.frame.contains(point)
    }
}

Upvotes: 1

Related Questions