arrays - 向数组/结构添加按钮?
问题描述
我有一个结构,运行时,它们显示在 tableviewcells 中,如下图所示:
是否可以将“浇头”(让国家,让奶酪,让蔬菜)添加为按钮而不是单元格内的字符串/文本?我尝试了很多不同的解决方案,但我认为我弄错了。如果它们是按钮,那么我可以向按钮/单元格添加一个功能,并将按钮的标题添加到表格视图下方的文本字段中。(在“文档大纲”中,单元格只有一个内容视图)希望我的问题有意义
import UIKit
class CountriesTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count:Int?
if tableView == self.tableView1 {
count = countries.count
}
if tableView == self.tableView2 {
count = cheeses.count
}
if tableView == self.tableView3 {
count = veges.count
}
return count!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell?
if tableView == self.tableView1 {
cell = tableView.dequeueReusableCell(withIdentifier: "CountryCell", for: indexPath)
let country = countries[indexPath.row]
cell!.textLabel!.text = country.name
cell?.textLabel?.textAlignment = .center
cell?.detailTextLabel?.text = country.isoCode
cell?.imageView?.image = UIImage(named: country.isoCode)
}
if tableView == self.tableView2 {
cell = tableView.dequeueReusableCell(withIdentifier: "CheCell", for: indexPath)
let che = cheeses[indexPath.row]
cell?.textLabel?.text = che.name
cell?.textLabel?.textAlignment = .center
cell?.detailTextLabel?.text = che.isoCode
cell?.imageView?.image = UIImage(named: che.isoCode)
}
if tableView == self.tableView3 {
cell = tableView.dequeueReusableCell(withIdentifier: "VegeCell", for: indexPath)
let veg = veges[indexPath.row]
cell!.textLabel!.text = veg.name
cell?.textLabel?.textAlignment = .center
cell?.detailTextLabel?.text = veg.isoCode
cell?.imageView?.image = UIImage(named: veg.isoCode)
}
return cell!
}
@IBOutlet weak var tableView3: UITableView!
@IBOutlet weak var tableView2: UITableView!
@IBOutlet weak var tableView1: UITableView!
struct cheese {
var isoCode: String
var name: String
}
let countries = [
cheese(isoCode: "", name: "Bacon"),
cheese(isoCode: "", name: "CHORIZO"),
cheese(isoCode: "", name: "Cocktail Pølser"),
cheese(isoCode: "", name: "Kalve"),
cheese(isoCode: "", name: "KEBAB"),
cheese(isoCode: "", name: "KYL"),
cheese(isoCode: "", name: "OKSEKØD"),
cheese(isoCode: "", name: "PEPPERONI"),
cheese(isoCode: "", name: "PARMA"),
cheese(isoCode: "", name: "SKINKE"),
cheese(isoCode: "", name: "TUN"),
cheese(isoCode: "", name: "REJER"),
]
let cheeses = [
cheese(isoCode: "", name: "OST"),
cheese(isoCode: "", name: "FRISK MOZZA"),
cheese(isoCode: "", name: "CHEDDAR"),
cheese(isoCode: "", name: "GORGONZOLA"),
cheese(isoCode: "", name: "PARMESAN"),
cheese(isoCode: "", name: "FETA"),
]
let veges = [
cheese(isoCode: "s", name: "FRISK TOMAT"),
cheese(isoCode: "", name: "CHERRY TOMAT"),
cheese(isoCode: "", name: "CHAMPIGNON"),
cheese(isoCode: "", name: "LØG"),
cheese(isoCode: "", name: "ANANAS"),
cheese(isoCode: "", name: "MAJS"),
cheese(isoCode: "", name: "OLIVEN"),
cheese(isoCode: "", name: "G. PEBERFRUGT"),
cheese(isoCode: "", name: "R. PEBERFRUGT"),
cheese(isoCode: "", name: "JALAPEÑOS"),
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView1.dataSource = self
tableView1.delegate = self
// tableView1.register(UITableViewCell.self, forCellReuseIdentifier: "CountryCell")
tableView2.dataSource = self
tableView2.delegate = self
// tableView2.register(UITableViewCell.self, forCellReuseIdentifier: "CheCell")
tableView3.dataSource = self
tableView3.delegate = self
// tableView3.register(UITableViewCell.self, forCellReuseIdentifier: "VegeCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var showTopping: UITextField!
@IBAction func addTopping(_ sender: UIButton) {
self.showTopping.text = sender.currentTitle
// }
// @IBAction func removeTopping(_ sender: UIButton) {
// append.self.showTopping.text = sender.currentTitle
}
}
解决方案
您需要创建一个自定义单元格,完成此操作后,您可以制作单元格按钮并在单元格中添加您想要的任何其他内容。自定义单元类具有无限的功能。
推荐阅读
- java - 如何 - 按键对列表进行分组并使用流 Java 8 拆分值
- c# - 如何从 Microsoft.Sharepoint.Client.ListItem 转换为 Microsoft.Sharepoint.Client.File?
- vb.net - 如何在datagridview vb.net中导出多个选定的行
- gnupg - gpg:选择卡失败:没有这样的设备
- lwjgl - MemoryStack#stackMalloc 和 MemoryStack stack = stackPush(); 有什么区别?堆栈.malloc?
- php - Symfony\Component\Routing\Exception\RouteNotFoundException; 路线 [home.getstarted] 未定义
- flutter - 当我在使用颤振条纹库时调用 mthod initPaymentSheet 时,颤振应用程序崩溃了
- python - 在while循环python中删除尾随逗号
- javascript - 如何在threejs中为单个glb文件添加多个纹理
- xmgrace - Xmgrace 通过批处理文件删除数据集中的特定行