Controlling when custom disclosure indicator is present in tableviewcell

I'm trying to control when a custom disclosure indicator is present in a cell. I'm specifically trying to do it using the one with the custom position (which is in my custom tableview cell class):

TableViewCell disclosure indicator position

I'm trying to control whether or not it's present in my cell, but since I'm using a custom tableview cell, it's been difficult for me. For the sake of it, here is the code from the post that I'm using:

class CellSubclass: UITableViewCell {

    var accessoryButton: UIButton?

    override func awakeFromNib() {
        super.awakeFromNib()
        accessoryType = .DisclosureIndicator
        accessoryButton = subviews.flatMap { $0 as? UIButton }.first
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        accessoryButton?.frame.origin.y = 15
    }
}

Here is my custom tableview cell class with that code worked into it:

protocol MainTableViewCellDelegate {
    func tappedReport(id: String)
    func cell(_ cell: MainTableViewCell, didTapReport reportID: String)

}

class MainTableViewCell: UITableViewCell {

    @IBOutlet weak var cellView: UIView!
    @IBOutlet weak var userLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var postLabel: UILabel!
    var accessoryButton: UIButton?

    var postItem: PostData!
    var delegate: MainTableViewCellDelegate?
    override func awakeFromNib() {
        accessoryType = .disclosureIndicator
        accessoryButton = subviews.flatMap { $0 as? UIButton }.first
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        accessoryButton?.frame.origin.y = 15
    }

    func setPost(post: PostData) {
        postItem = post
        let ourPost = postItem.postId
        print(ourPost)
    }


    @IBAction func reportButton(_ sender: UIButton) {
        delegate?.tappedReport(id: "3")
        delegate?.cell(self, didTapReport: "Test")
    }

}

Now, in my tableview in the cellForRowAt method, how do I turn this on/off? It shows up fine in every cell, but I can't think of how to make a protocol or something like that to control when this shows up and doesn't...Thanks in advance for the help! (The reason I can't just do this in my cellForRowAt method is because I can't have my ViewController being a subclass of UITableViewCell and UIViewController at the same time)

1 answer

  • answered 2018-02-13 00:29 tereks

    I see two ways of implementing that:

    1. In cellForRowAt method you can call accessoryButton.isHidden = true or false
    2. More preferred way - create your own custom UIButton with arrow image and place it on disclosure indicator's position. And set visibility via isHidden flag.