我在 ViewController 中有一個 tableView,它也有一個名為 infoView 的單獨視圖。我沒有第二個 viewController。infoView 和 tableView 在同一個 ViewController 中,當點擊特定單元格時,我需要 infoView 中的標簽來更新。我創建了一個單獨的函式來更新標簽,但是每當我呼叫該函式時,它什么都不做。這是我的 viewController 的螢屏截圖,以便更好地理解。 視圖控制器
這是我的代碼:
@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.
}
我希望這個問題是有道理的。提前致謝!
uj5u.com熱心網友回復:
很好的第一次嘗試,我強烈建議您探索UITableViewDelegate 在那里您可以查看您的 ViewController 是否是表格視圖的委托,您可以執行類似的操作
extension MyViewController: UITableViewDelegate {
func tableView(UITableView, didSelectRowAt: IndexPath) {
updateLabels(elementArray[indexPath.row])
}
}
由于您似乎對 iOS 開發較新,我還要提到您應該將此視圖控制器設定為您的表視圖的委托,這意味著當某些事件發生時,表視圖將呼叫您的視圖控制器的函式。您可以如何執行此操作的一個示例是:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
}
但是,如果您的視圖控制器是 UITableViewController 的子類,只需將該函式添加到您的類中。
uj5u.com熱心網友回復:
注意:添加 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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/405819.html
標籤:
下一篇:嘗試除錯iOS/iPadOSAppleSiliconMacApp時willDisplayHeaderView屬性始終為零
