2017-03-01 5 views
0

MapBoxとTableViewで次のデザインを実装しようとしています。MapView [Swift]で3つの "列"を持つUITableViewを実装する

Screenshot of the desing

私はそれについて考えてきたと私は結果を得るためのUITableViewを使用したかったが、私の知る限りでは、それは左&右側にデータやディテールを持っていることだけが可能です。 UITableViewの代替手段はありますか?

ない場合は、私も自分の「ルート」-Viewは(MapBoxから)MapViewであると私はUITableViewControllerとしてもUITableViewDelegate/UITableViewDataSourceとしてMapViewControllerを使用することができない問題に直面しています。 MapViewを別のビューに埋め込むことは可能ですか?

さらに詳しい情報が必要な場合は、私に知らせてください。そして、事前に感謝します。

+1

は、あなただけの独自のカスタム 'UITableViewCell' –

+0

を実装する必要があります' UITableViewController'だけの便利なクラスです - すべてを行う必要が置かれていますあなたのMapControllerの 'UITableView'へのアウトレットを開き、その中に' UITableViewDataSource'と 'UITableViewDelegate'ルーチンを実装します。テーブルの列に関しては、あなた自身の 'UITableViewCell'のサブクラスを実装する必要があります。 Ray Wenderlichはこれに関する一連のチュートリアルを行っています。 – Grimxn

+0

@Grimxn私は、実装を'UITableViewController 'として、または'Delegate'& 'DataSource'と同じファイル内に見ただけです。ルーチンの実装例はありますか?それは素晴らしいだろう... – njoye

答えて

0

私が知る限り、左側のデータと詳細は右側にあることが可能です&右側。代替品はありますか?

あなたは間違っています。任意のインターフェイスをテーブルビューセルに含めることができます。これをカスタムセルにして、必要に応じてデザインします。

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 ... 

} 

:あなたは正しく次のコードに2つのコンセントに接続UITableViewMKMapView(または使用しているものは何でも)の両方を持っているViewControllerをストーリーボードまたはXIBでUIViewControllerと呼ばれていると仮定すると

0

次に、次のコードを作成する必要があります。スタンドアロンのxib(この場合は「MyCell.xib」と呼ばれます)を作成する必要があります。 xibには、テーブルセルに必要なすべてのコントロールが含まれている必要があります。私の例では、コントロールは1つしかありません。UILabelcellNumberと参照されています。

xibを作成するには、Xcodeメニューから「File-> New File-> User Interface-> Empty」を選択し、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` 
    } 
} 
あなたがテーブルビューセルに好きな場所あなたがデータを持つことができ
関連する問題