I have a tableView inside of a ViewController that also has a separate view called infoView. I do not have a second viewController. The infoView and the tableView are in the same ViewController and I need the labels in the infoView to update when a specific cell is tapped. I have created a separate function to update the labels but whenever I call the function it doesn’t do anything. Here is a screenshot of my viewController for a better understanding.
ViewController
Here is the code I have:
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var symbol: UILabel!
@IBOutlet weak var elementName: UILabel!
@IBOutlet weak var atomicNum: UILabel!
@IBOutlet weak var atomicWeight: UILabel!
@IBOutlet weak var meltingPoint: UILabel!
@IBOutlet weak var boilingPoint: UILabel!
@IBOutlet weak var yearDiscovered: UILabel!
@IBOutlet weak var textView: UITextView!
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(selectedCell))
//Info Source - Wikipedia because I don't really care if it's correct.
//Temps are in Fahrenheit
//I realize this isn't a complete list of radioactive elements. Don't Science Me!
let elementArray:[(symbol: String, name: String, atomicNum: Int, atomicWeight:Double, meltPoint:Double?, boilPoint:Double?, notes:String, discYear:Int )] = [
("Pu", "Plutonium", 94, 244, 1182.9, 5842, "Prepared by bombardment of uranium with deuterons.", 1941),
("U", "Uranium", 92, 238.02891, 2070, 4404, "Mistakenly identified a uranium oxide obtained from pitchblende as the element itself and named it after the recently discovered planet Uranus.", 1789),
("Pm", "Promethium", 61, 145, 1908, 5432, "It was probably first prepared in 1942 by bombarding neodymium and praseodymium with neutrons, but separation of the element could not be carried out. Isolation was performed under the Manhattan Project in 1945.", 1942),
("Rn", "Radon", 86, 222, nil, nil, "E. Dorn discovered a radioactive gas resulting from the radioactive decay of radium, isolated later by W. Ramsay and R.W. Gray.", 1898),
("Th", "Thorium", 90, 232.0377, 3182, 8650, "J. Berzelius obtained the oxide of a new earth in thorite.", 1829),
("Ra", "Radium", 88, 226, 1760, 3159, "The Curies reported on December 26, 1898, a new element different from polonium, which Marie later isolated from uraninite.", 1898),
("Ds", "Darmstadtium", 110, 281, nil, nil, "Prepared by bombardment of lead with nickel.", 1994),
("Po", "Polonium", 84, 209, 489, 1764, "In an experiment done on July 13, 1898, the Curies noted an increased radioactivity in the uranium obtained from pitchblende, which they ascribed to an unknown element.", 1898),
("Fr", "Francium", 87, 223, 80 , 1250, "Perey discovered it as a decay product of Ac. Francium is the last element to be discovered in nature, rather than synthesized in the lab, although some of the synthetic elements that were discovered later (plutonium, neptunium, astatine) were eventually found in trace amounts in nature as well.", 1939),
("Ac", "Actinum", 89, 227, 2240, 5800, "A.L. Debierne obtained from pitchblende a substance that had properties similar to those of thorium.", 1899),
("Cm", "Curium", 96, 247, 2444, 5630, "Prepared by bombarding plutonium with alpha particles during the Manhattan Project", 1944),
("Es", "Einsteinium", 99, 252, 1580, 1825, "Formed in the first thermonuclear explosion in November 1952, by irradiation of uranium with neutrons; kept secret for several years.", 1952)
]
func numberOfSections(in tableView: UITableView) -> Int { return 1; }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elementArray.count}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "elementCell", for: indexPath) as! ElementTableViewCell
cell.setupCell(elementArray[indexPath.row].symbol, name: elementArray[indexPath.row].name)
cell.addGestureRecognizer(tapGesture)
return cell
}
@objc func selectedCell() {
//display selected cell info in info View
if let indexpath = tableView.indexPathForSelectedRow {
let element = elementArray[indexpath.row]
symbol.text = element.symbol
elementName.text = element.name
atomicNum.text = element.atomicNum.description
atomicWeight.text = element.atomicWeight.description
yearDiscovered.text = element.discYear.description
textView.text = element.notes
if let boiling = element.boilPoint {
boilingPoint.text = boiling.description
} else {
boilingPoint.text = "N/A"
}
if let melt = element.meltPoint {
meltingPoint.text = melt.description
} else {
meltingPoint.text = "N/A"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
I hope this question makes sense. Thanks in advance!
2
Answers
Good first attempt, I would highly recommend you explore UITableViewDelegate
There you can see if your ViewController is the table view’s delegate you can do something similar to
Since you seem newer to iOS Development I’ll also mention you should make this view controller a delegate of your table view, which means the table view will call functions of your viewController when certain events happen.
an example of how you might do this is:
However, if your view controller is a subclass of UITableViewController just add the function to your class.
Note: Add didSelectRowAt method this is one of the delegate method of UITableview which is called when you tap on the cell.