问题描述
我正在尝试在 UITableViewCell 的附件视图中添加自定义按钮.
I am trying to add a custom button in the Accessory View for a UITableViewCell.
我添加了以下代码:
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:
问题可能是按钮没有大小,所以它不可见.添加这一行,在设置图像之后:
button.sizeToFit()
另一种可能是您没有名为 "arrow-right-light.png"
的图像.这不会使您现有的代码崩溃,但会阻止按钮具有任何图像,因此您将看不到它.尝试使用 UIImage(named: "arrow-right-light.png")!
代替,作为测试;如果您崩溃,那确实是问题所在,然后您就可以找出正确的名称.
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.
这篇关于UITableViewCell 中的自定义附件按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!