I am trying to add a custom button in the Accessory View for a UITableViewCell.
I have added the following code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// ....
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "arrow-right-light.png")?.maskWithColor(color: .white), for: .normal)
button.addTarget(self, action: #selector(buttonTest), for: .touchUpInside)
button.tag = indexPath.row
cell.accessoryView = button
// ...
However, I don't see the button show up on in the Accessory View.
I can think of two possible reasons:
The problem might be that the button has no size, so it's invisible. Add this line, after setting the image:
button.sizeToFit()
Another possibility is that you have no image named "arrow-right-light.png"
. That wouldn't crash your existing code, but it would prevent the button from having any image so you wouldn't see it. Try saying UIImage(named: "arrow-right-light.png")!
instead, just as a test; if you crash, that was indeed the problem, and then you can figure out what the correct name is.