更改 UITableViewCell 中详细信息按钮的图像
Change image of detail button inside UITableViewCell
我有这个 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
,如果找到,则更改其图像。但它始终找不到按钮!
查看层次结构
提前致谢。
我认为不需要循环,只需使用 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 的回答,我做了一个 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
附件:
我有这个 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
,如果找到,则更改其图像。但它始终找不到按钮!
查看层次结构
提前致谢。
我认为不需要循环,只需使用 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 的回答,我做了一个 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
附件: