I made a simple app for practicing tableView and Singletons. I am new to swift so I would appreciate some quick help on this one. The thing I want to do is simple but I can't think of anything at the moment.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var tableView = UITableView()
let names = Singleton.shared.nameArray
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = .init(x: 0, y: 0, width: 414, height: 260)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let name = names[indexPath.row]
cell.textLabel?.text = "cell \(indexPath.row + 1)"
return cell
}
@IBAction func didPressButton(_ sender: Any) {
}
}
basically I want the tableView label to change to this name
constant i made after the button is clicked.
Sorry if I didn't make everything clear in the question and I hope that you will understand and be able to help me. It's kinda hard to explain the code because I'm new to swift and English isn't my native language.
Edited to clear things up a little bit.
Add to the ViewController:
private var areNamesVisible = false
@IBAction func didPressButton(_ sender: Any) {
areNamesVisible.toggle()
tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let name = names[indexPath.row]
cell.textLabel?.text = areNamesVisible ? name : "cell \(indexPath.row + 1)"
return cell
}