如何 link 两个视图控制器以编程方式创建 table 视图?
How to link two view controllers to a programmatically created table view?
抱歉我的英语不好。
我想要这个:当有人点击一个单元格时,它会在另一个视图控制器中打开。
我以编程方式创建了 table 视图。
import UIKit
class BibleBooksViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
private let myArray: NSArray = ["Gênesis", "Êxodo", "Levitico"]
private var myTableView: UITableView!
var bibleArray = BibleBooksMock().booksArray
override func viewDidLoad() {
super.viewDidLoad()
let barHeight: CGFloat =
UIApplication.shared.statusBarFrame.size.height
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width:
displayWidth, height: displayHeight - barHeight))
myTableView.register(UITableViewCell.self, forCellReuseIdentifier:
"MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section:
Int) -> Int {
return bibleArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {
return 70
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for:
indexPath as IndexPath)
cell.textLabel!.text = "\(bibleArray[indexPath.row].title)"
return cell
}
}
输出是这样的:
iOS版本是12.1
swift 版本是 4.2
将此添加到 BibleBooksViewController
:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let chapter = bibleArray[indexPath.row]
var vc = ChapterViewController()
vc.chapterName = chapter.title
navigationController?.pushViewController(vc, animated: true)
}
并定义这个控制器:
class ChapterViewController: UIViewController {
var chapterName: String?
override func viewDidLoad() {
super.viewDidLoad()
title = chapterName
view.backgroundColor = UIColor.white
}
}
更多关于 tableView(_:didSelectRowAt:)
here。
当我们点击单元格时,在 tableview 中使用此代码 didselect 方法此方法用于操作
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
vc.strText = "\(arrData[indexPath.row])"
self.present(vc, animated: true, completion: nil)
OR
self.navigationController?.pushViewController(vc, animated: true)
}
当我们必须传递 ViewController 是 DetailViewController 的数据或导航时
class DetailViewController: UIViewController {
var strText: String?
override func viewDidLoad() {
super.viewDidLoad()
yourlabel.text = strText
}
}
在我的代码中,我使用了 DetailViewController 作为故事板 ID,所以在故事板中设置它就像在 SS 中一样。
你可以在 youtube 上查看我的视频 :-
https://www.youtube.com/watch?v=4MOiU_Qop-0
抱歉我的英语不好。
我想要这个:当有人点击一个单元格时,它会在另一个视图控制器中打开。
我以编程方式创建了 table 视图。
import UIKit
class BibleBooksViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
private let myArray: NSArray = ["Gênesis", "Êxodo", "Levitico"]
private var myTableView: UITableView!
var bibleArray = BibleBooksMock().booksArray
override func viewDidLoad() {
super.viewDidLoad()
let barHeight: CGFloat =
UIApplication.shared.statusBarFrame.size.height
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width:
displayWidth, height: displayHeight - barHeight))
myTableView.register(UITableViewCell.self, forCellReuseIdentifier:
"MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section:
Int) -> Int {
return bibleArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {
return 70
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for:
indexPath as IndexPath)
cell.textLabel!.text = "\(bibleArray[indexPath.row].title)"
return cell
}
}
输出是这样的:
iOS版本是12.1 swift 版本是 4.2
将此添加到 BibleBooksViewController
:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let chapter = bibleArray[indexPath.row]
var vc = ChapterViewController()
vc.chapterName = chapter.title
navigationController?.pushViewController(vc, animated: true)
}
并定义这个控制器:
class ChapterViewController: UIViewController {
var chapterName: String?
override func viewDidLoad() {
super.viewDidLoad()
title = chapterName
view.backgroundColor = UIColor.white
}
}
更多关于 tableView(_:didSelectRowAt:)
here。
当我们点击单元格时,在 tableview 中使用此代码 didselect 方法此方法用于操作
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
vc.strText = "\(arrData[indexPath.row])"
self.present(vc, animated: true, completion: nil)
OR
self.navigationController?.pushViewController(vc, animated: true)
}
当我们必须传递 ViewController 是 DetailViewController 的数据或导航时
class DetailViewController: UIViewController {
var strText: String?
override func viewDidLoad() {
super.viewDidLoad()
yourlabel.text = strText
}
}
在我的代码中,我使用了 DetailViewController 作为故事板 ID,所以在故事板中设置它就像在 SS 中一样。