假设你有你的故事板或XIB一个UIViewController
称为ViewController
其中既有UITableView
和MKMapView
(或任何你正在使用)正确连接到两个出口在下面的代码:
import UIKit
import MapKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Now tell the system that we are going to reference our
// hand-made table cell from a xib called "MyCell"
self.tableView.register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell")
// These next you can do here, or in IB...
self.tableView.dataSource = self
self.tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - TableViewDataSource
// ANY ViewController can do this, if we register the class as conforming
// to the `UITableViewDataSource` protocol
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a reference to an instance of our very own UITableViewCell subclass,
// Which we registered in `viewDidLoad`
let c = self.tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
// Whatever controls/outlets we have put in our cell subclass,
// we need to populate with data now... (I just did a label)
c.cellNumber?.text = "\(indexPath.row)"
return c
}
//MARK: - TableViewDelegate
//... implement whatever funcs you need ...
//MARK: - MKMapViewDelegate
//... implement whatever funcs you need ...
}
然后,您需要创建以下代码,并创建一个独立的xib(在本例中称为“MyCell.xib”)。 xib应该包含您在表格单元格中想要的所有控件。在我的示例中,它只有一个控件,UILabel
引用为cellNumber
。
要制作xib,请从Xcode菜单中选择“文件 - >新建文件 - >用户界面 - >空白”,然后从调色板中将UITableViewCell
拖放到xib中。确保将单元格的类别从UITableViewCell
更改为MyCell
。添加你需要的任何控件(和它们之间的约束)。显然,将这个类的所有控件连接到相关的@IBOutlets
。
class MyCell: UITableViewCell {
// Create `@IBOutlet weak var ...` for all of the controls in your cell here
@IBOutlet weak var cellNumber: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.configureCell()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.configureCell()
}
func configureCell() {
// Your stuff to load up the IBOutlet controls of your cell with defaults.
// You will be able to override these when the instantiated cell is passed to
// `tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell`
// in the `UITableViewDataSource`
}
}
无论你在表格视图单元格希望你可以有数据,你只需要实现自己的自定义'UITableViewCell' –
'UITableViewController'仅仅是一个方便的类 - 所有你需要做的就是把一个出口到你的MapController中的“UITableView”,并在其中实现“UITableViewDataSource”和“UITableViewDelegate”例程。至于表中的列,你需要实现你自己的'UITableViewCell'的子类。 Ray Wenderlich就此做了一系列很好的教程。 – Grimxn
@Grimxn我还没有看到'UITableViewController'的实现或与'Delegate'和'DataSource'在同一个文件中 - 你有一个例程实现的例子吗?那太棒了... – njoye