ios - UITableViewCell 中的自定义附件按钮

标签 ios swift uitableview accessoryview

我正在尝试在 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/

相关文章:

ios - 添加了 insertRowsAtIndexPaths 的 UITableView 单元格未正确绘制/布局

iphone - 添加 tableview 作为 subview (如果你必须从 UITableVC 继承)

ios - 如何在核心图中设置绘图委托(delegate)

ios - 在设备中查找路径时出错

cocoa-touch - UITableViewCell setSelected 方法没有被调用

ios - 当我使用 ReactiveKit/Bond 调用 becomeFirstResponder 后,UITextField 立即退出第一响应者

objective-c - XPC服务支持多种协议(protocol)

Ios Swift 解码从 GCM 通知发送的 json 数据

iphone - 每次启动应用程序时播放声音

c - Swift - C API 桥 - 如何处理空指针