[英]How do I update labels based on a selected tableView cell that is in the same View Controller?
I have a tableView inside of a ViewController that also has a separate view called infoView.我在 ViewController 中有一个 tableView,它也有一个名为 infoView 的单独视图。 I do not have a second viewController.
我没有第二个 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.
infoView 和 tableView 在同一个 ViewController 中,当点击特定单元格时,我需要 infoView 中的标签来更新。 I have created a separate function to update the labels but whenever I call the function it doesn't do anything.
我创建了一个单独的 function 来更新标签,但是每当我调用 function 时它什么都不做。 Here is a screenshot of my viewController for a better understanding.
这是我的 viewController 的屏幕截图,以便更好地理解。 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!
提前致谢!
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很好的第一次尝试,我强烈建议您探索UITableViewDelegate在那里您可以查看您的 ViewController 是否是表格视图的委托,您可以执行类似的操作
extension MyViewController: UITableViewDelegate {
func tableView(UITableView, didSelectRowAt: IndexPath) {
updateLabels(elementArray[indexPath.row])
}
}
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.由于您似乎对 iOS 开发较新,我还要提一下您应该将此视图 controller 设置为表视图的委托,这意味着表视图将在某些事件发生时调用您的 viewController 的函数。 an example of how you might do this is:
您可以如何执行此操作的一个示例是:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
}
However, if your view controller is a subclass of UITableViewController just add the function to your class.但是,如果您的视图 controller 是 UITableViewController 的子类,只需将 function 添加到 class 即可。
Note: Add didSelectRowAt method this is one of the delegate method of UITableview which is called when you tap on the cell.注意:添加 didSelectRowAt 方法,这是 UITableview 的委托方法之一,当您点击单元格时会调用该方法。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let items = elementArray[indexPath.row]
updateInfo(item: items)
}
func updateInfo(item : (symbol: String, name: String, atomicNum: Int, atomicWeight:Double, meltPoint:Double?, boilPoint:Double?, notes:String, discYear:Int ))
{
symbol.text = item.symbol
elementName.text = item.name
atomicNum.text = item.atomicNum.description
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.