我來這里尋求幫助,解決這個應用程式開發程序中出現的一個小問題。
我對 Swift 的經驗很少,我正在撰寫一些代碼來了解更多資訊。我有這個應用程式,它在初始螢屏中有一組按鈕,可以重定向到不同的頁面和功能。
名為“Table View (CH Cantons)”的按鈕重定向到一個新視圖,其中包含一個向我們展示三件事的 TableView:
- 州徽或州徽
- 廣州,如標題:例如:ZH - 蘇黎世
- 特定州的面積
選擇一個州后,應用程式應轉發到新頁面并顯示有關該特定州的詳細資訊,但盡管我努力解決問題,但這并沒有發生。TableView 是否需要在主視圖/主頁中?

波紋管是與導航控制器的連接

生成表視圖的代碼
let cantonsData = ["AG - Aargau","BE - Bern","GE - Geneve","GR - Graubunden","JU - Jura","LU - Lucern","NE - Neuchatel","SG - Sankt Gallen","TG - Thurgau","TI - Ticino","UR - Uri","VD - Vaud","VS - Valais","ZH - Zurich","AI - Appenzell \n Innerrhoden","AR - Appenzell \n Ausserrhoden","SH - Schaffhausen","BL - Basel \n Landschaft","BS - Basel","SO - Solothurn","FR - Fribourg","ZG - Zug","GL - Glarus","NW - Nidwald","OW - Obwald", "SZ - Schwyz"]
let area = ["Area (km2): 1404 km2","Area (km2): 5,960 km2","Area (km2): 1,792 km2","Area (km2): 7,105 km2","Area (km2): 839 km2","Area (km2): 1,494 km2","Area (km2): 802 km2 ","Area (km2): 2031 km2","Area (km2): 992 km2","Area (km2): 2812 km2","Area (km2): 1077 km2","Area (km2): 3212 km2","Area (km2): 5224 km2","Area (km2): 1729 km2","Area (km2): 172 km2","Area (km2): 243 km2","Area (km2): 298 km2","Area (km2): 518 km2","Area (km2): 37 km2","Area (km2): 790 km2","FR - Fribourg","ZG - Zug","GL - Glarus","NW - Nidwald","OW - Obwald", "SZ - Schwyz"]
override var modalPresentationStyle: UIModalPresentationStyle {
get { .fullScreen }
set { assertionFailure("Shouldnt change that ??") }
}
override func viewDidLoad() {
//super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = "CH Cantons / CH Kantons"
tableView.dataSource = self
tableView.delegate = self
let nibName = UINib(nibName: "TableViewCell", bundle: nil)
tableView.register(nibName, forCellReuseIdentifier: "tableViewCell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cantonsData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
cell.commonInit("CH-\(indexPath.item 1)", title: cantonsData[indexPath.item], sub: area[indexPath.item])
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 180
}
選擇表格視圖的單元格時負責打開新頁面的函式
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "TableViewCH", bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController{
vc.img = UIImage(named: cantonsData[indexPath.item])!
vc.canton = cantonsData[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
}
我嘗試了另一個但無法解決問題
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "mySegue", sender: cell)
}
uj5u.com熱心網友回復:
嘗試這個:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "TableViewCH", bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController{
vc.img = UIImage(named: cantonsData[indexPath.item])!
vc.canton = cantonsData[indexPath.row]
let nav = UINavigationController(rootViewController: vc)
nav.modalPresentationStyle = .overFullScreen
self.present(nav, animation: true)
}
}
uj5u.com熱心網友回復:
正如其他人所提到的,您正在以模態方式呈現您的螢屏(這意味著它更像是一個彈出視窗并且沒有 ViewController 堆疊)。要解決此問題,我將嘗試進入 Storyboard 檔案并選擇NavigationController并選擇presentation屬性底部附近的選項,將其更改為over current context
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505318.html
上一篇:Swift條件陳述句(ifelse)中下劃線“_”的作用
下一篇:如何使計時器每10秒重置回0秒?
