2017-03-01 56 views
0

我试图用MapBox和TableView实现以下设计。在MapView中使用三个“列”实现UITableView [swift]

Screenshot of the desing

我一直在想这个问题,我想用一个UITableView的结果,但据我所知,这只是可能有左侧&右侧的数据和细节。有没有替代UITableView?

如果没有,我也面临着我的“根” - 视图是一个MapView(从MapBox),我也可以不使用MapViewControllerUITableViewController也不是UITableViewDelegate/UITableViewDataSource问题。是否有可能在另一个视图中嵌入MapView

如果您需要更多信息,请让我知道。并预先感谢您。

+1

无论你在表格视图单元格希望你可以有数据,你只需要实现自己的自定义'UITableViewCell' –

+0

'UITableViewController'仅仅是一个方便的类 - 所有你需要做的就是把一个出口到你的MapController中的“UITableView”,并在其中实现“UITableViewDataSource”和“UITableViewDelegate”例程。至于表中的列,你需要实现你自己的'UITableViewCell'的子类。 Ray Wenderlich就此做了一系列很好的教程。 – Grimxn

+0

@Grimxn我还没有看到'UITableViewController'的实现或与'Delegate'和'DataSource'在同一个文件中 - 你有一个例程实现的例子吗?那太棒了... – njoye

回答

0

但据我所知,这是只可能有左&右侧的数据和细节。是否有替代方案

你知道错了。你可以在表格视图单元格中包含你想要的任何接口。只需将其制作为自定义单元并根据需要进行设计即可。

0

假设你有你的故事板或XIB一个UIViewController称为ViewController其中既有UITableViewMKMapView(或任何你正在使用)正确连接到两个出口在下面的代码:

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` 
    } 
} 
相关问题