我正在尝试在 UITableViewCell 的附件 View 中添加自定义按钮。
我添加了以下代码:
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
// ...
但是,我没有在附件 View 中看到该按钮。
最佳答案
我能想到两个可能的原因:
问题可能是按钮没有大小,所以它是不可见的。添加此行,在设置图像之后:
button.sizeToFit()
另一种可能是您没有名为
"arrow-right-light.png"
的图像。这不会使您现有的代码崩溃,但会阻止按钮显示任何图像,因此您看不到它。尝试说UIImage(named: "arrow-right-light.png")!
作为测试;如果你崩溃了,那确实是问题所在,然后你可以找出正确的名称。
关于ios - UITableViewCell 中的自定义附件按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949150/