【问题标题】:Change image of detail button inside UITableViewCell更改 UITableViewCell 内详细信息按钮的图像
【发布时间】:2020-11-08 12:16:40
【问题描述】:

我有这个UITableViewCell

我想要这样的结果:(请原谅我的错误编辑)

这意味着基本上将detail button的图像更改为另一个图像。

我当前的单元格代码

let cell = UITableViewCell(style: .default, reuseIdentifier: nil)

cell.textLabel?.text = NSLocalizedString("New", comment: "Add a new element")
cell.imageView?.image = UIImage(systemName: "plus.circle.fill")

cell.backgroundColor = .secondarySystemBackground
        
cell.imageView?.preferredSymbolConfiguration = .init(scale: UIImage.SymbolScale.large)
        
cell.selectionStyle = .none
        
cell.accessoryType = .detailButton
        

我尝试过的

我把它放在上面的代码之后。 (它不起作用)

        for (i, view) in cell.subviews.enumerated() {
            if view as? UIButton != nil {
                (cell.subviews[i] as! UIButton).setImage(UIImage(systemName: "clock"), for: .normal) // Never executes
            } else {
                for (i, view) in view.subviews.enumerated() {
                    if view as? UIButton != nil {
                        (cell.subviews[i] as! UIButton).setImage(UIImage(systemName: "clock"), for: .normal) // Never executes
                    }
                }
            }
        }

此代码在单元格的subviews 中搜索UIButton,如果找到,则更改其图像。但它永远找不到按钮!

查看层次结构

提前致谢。

【问题讨论】:

    标签: swift uitableview uibutton


    【解决方案1】:

    根据@zeytin 的回答,我做了一个UITableViewCell 扩展,以便于使用,同时也提高了利润率:

    extension UITableViewCell {
    /// Set accessory image to any UIImage.
    /// - Made by:
    ///  Ori HPT
    /// - Parameters:
    ///   - image: The image to set.
    ///   - color: The tint color of the image.
    ///   - selector: The action of the accessory.
    ///   - target: The target of the action. (self)
    func setAccessoryImage(to image: UIImage, color: UIColor, selector: Selector?, target: Any?) {
        self.accessoryType = .none
    
        let button = UIButton(type: .custom)
        button.setImage(image, for: .normal)
        let size = self.textLabel?.font.pointSize ?? UIFont.preferredFont(forTextStyle: .body).pointSize
        button.setPreferredSymbolConfiguration(.init(pointSize: size, weight: .regular, scale: UIImage.SymbolScale.large), forImageIn: .normal)
        button.sizeToFit()
        if selector != nil {
            button.addTarget(target, action: selector!, for: .touchUpInside)
        }
        button.tintColor = color
        self.accessoryView = button
    }
    }
    

    使用示例:

    cell.setAccessoryImage(to: UIImage(systemName: "clock")!, color: .systemBlue, selector: #selector(historyPressed), target: self)
    

    这是最终结果,对比系统的.detailButton附件:

    【讨论】:

      【解决方案2】:

      我认为不需要循环,只需使用 cellForRow 函数并设置单元格的 accessorView 属性即可。

      func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
      
         // Other code...
      
         cell.accessoryType = .none
              
         let button = UIButton(type: .custom)
         button.setImage(UIImage(systemName: "clock"), for: .normal)
         button.setPreferredSymbolConfiguration(.init(scale: UIImage.SymbolScale.large), forImageIn: .normal)
         button.sizeToFit()
         button.addTarget(self, action: #selector(historyPressed), for: .touchUpInside)
         cell.accessoryView = button
      
         return cell
      }
      
      @objc func historyPressed() {
          print("History button pressed")
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-14
        • 1970-01-01
        • 2014-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多