[英]How to add icons(or custom images) to right side of table view cell in Swift
I am trying to add some icons to right side of a tableView cell.我正在尝试在 tableView 单元格的右侧添加一些图标。 You can imagine it like "mute" icon of WhatsApp but it does not show icons in cells.
您可以将其想象为 WhatsApp 的“静音”图标,但它不会在单元格中显示图标。 Here is my code:
这是我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var content = cell.defaultContentConfiguration()
let countryNames = Array(countryInfos.keys)
content.text = countryNames[indexPath.row]
cell.contentConfiguration = content
let myImage = UIImage(named:"star")
cell.imageView?.image = myImage
return cell
}
I tried like this also:我也试过这样:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
var content = cell.defaultContentConfiguration()
let countryNames = Array(countryInfos.keys)
content.text = countryNames[indexPath.row]
cell.contentConfiguration = content
let myImage = UIImage(named:"star")
cell.imageView?.image = myImage
return cell
}
How can I make it visible and add it to the right side of a cell?如何使其可见并将其添加到单元格的右侧?
Note: star image is in the "Assets" folder as star.png注意:星形图像在“资产”文件夹中为star.png
I think you can use custom cell and use stackView for text and image:我认为您可以使用自定义单元格并将 stackView 用于文本和图像:
class CustomCell: UITableViewCell {
var customLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var customImage: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
var stackView: UIStackView = {
let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .horizontal
stack.distribution = .fill
stack.alignment = .fill
return stack
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
stackView.addArrangedSubview(customLabel)
stackView.addArrangedSubview(customImage)
addSubview(stackView)
setupConstraints()
}
func setupConstraints(){
stackView.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
stackView.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
stackView.topAnchor.constraint(equalTo: topAnchor, constant: 5).isActive = true
stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
then you have to register the cell in viewDidLoad那么你必须在 viewDidLoad 中注册单元格
tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
and use it like so:并像这样使用它:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CustomCell {
cell.customLabel.text = countries[indexPath.row]
cell.customImage.image = UIImage(systemName: "star")
return cell
}
return UITableViewCell()
}
Register your cell:注册您的手机:
YourTableViewName.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
Then use your CustomCell:然后使用您的 CustomCell:
To access your CustomCell image, do:要访问您的 CustomCell 图像,请执行以下操作:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = YourTableViewName.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell else {
return UITableViewCell()}
let myImage = UIImage(named:"star")
cell.imageView?.image = myImage
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.