Swift 更改UITableViewCell内详细信息按钮的图像

Swift 更改UITableViewCell内详细信息按钮的图像,swift,uitableview,uibutton,Swift,Uitableview,Uibutton,我有一个UITableViewCell: 我希望结果是:(请原谅我的错误编辑) 这意味着基本上将详细信息按钮的图像更改为另一个图像 我当前的手机代码 我试过的 我把这个放在上面的代码后面。(不起作用) 此代码在单元格的子视图中搜索ui按钮,如果找到,则更改其图像。但它从来没有找到一个按钮 视图层次结构 提前感谢。我认为不需要循环,只需使用cellForRow函数并设置单元格的accessorView属性即可 func tableView(tableView: UITableView!,

我有一个
UITableViewCell

我希望结果是:(请原谅我的错误编辑)

这意味着基本上将
详细信息按钮的图像更改为另一个图像

我当前的手机代码 我试过的 我把这个放在上面的代码后面。(不起作用)

此代码在单元格的
子视图中搜索
ui按钮
,如果找到,则更改其图像。但它从来没有找到一个按钮

视图层次结构


提前感谢。

我认为不需要循环,只需使用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")
}

基于@zeytin answer,我做了一个
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
附件相比,这是最终结果:

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)