Reputation: 49
I have a struct and when run, they are shown in the tableviewcells as in these pictures:
Is it possible to have the "toppings" (let countries,let cheeses, let veges) be added as buttons instead of string/text within the cell? I have tried quite a few different solutions, but I think Ive got it wrong somehow. If they were buttons then I could add a function to the buttons/cells, and add the title of the button to the textfield below the tableviews. (in the "document outline" the cells only have a content view) Hope my question makes sense
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
}
}
Upvotes: 0
Views: 71
Reputation: 62
You need to create a custom Cell, Once this is done you can make the cells buttons and add anything else you would like within a cell. There is unlimited functionality with custom cell class.
Upvotes: 0