一般的なUIDIewControllerでUItable Viewを使用する方法

1703 ワード

このシリーズの文章「Swift on iOS学習ノート」は、不定長、不定内容、不定形式で公開され、主に「再利用可能」な知識を記録し、読書に感謝します.
UIdiewControllerから継承された通常のページでUItableViewを使用することは、UItableViewControllerのカスタマイズ性が悪いため、非常に一般的なニーズです.あまり話さないで、すぐに始めます.
1.新規アプリケーション
2.Table Viewを追加
3.Table ViewにTable View Cellを追加
4.左側でTable View Cellを選択し、Identifierを付与
左側をクリックして選択:
右側、Identifierボックスに小文字のcellを入力し、入力が完了したらEnterを押して確認してください.
 
5.このTable Viewをコードにバインドする
firstTableViewという名前です.
6.コードの修正
import UIKit



class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {



    @IBOutlet weak var firstTableView: UITableView!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        firstTableView.delegate = self

        firstTableView.dataSource = self

    }



    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 2

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        

        cell.textLabel.text = "    \(indexPath.row)  Cell"

        

        return cell

    }





}


7.運転:
8.まとめ
このfirstTable Viewはページ上の普通のviewにすぎず、彼のサイズと位置を直接調整したり、他のviewを勝手に追加したりすることができます.