Populate table view in swift

后端 未结 2 2032
栀梦
栀梦 2021-02-13 13:45

Hi i want to populate this tableview \"configuration

i don\'t use the static table view because i tak

2条回答
  •  死守一世寂寞
    2021-02-13 14:26

    Oh TableViews...

    So, you want to dynamically populate a tableview. Well... here you go my friend:


    Step 1: Implement The DataSource

    Okay.... so you know what a protocol is? Well if you don't ... BAM, now you do (Not me in video). A DataSource is what the UITableView will call to retrieve the data it requires to display. To be specific, the DataSource in question is UITableViewDataSource.

    so...

        // change top to...
    
        class MyViewController:UIViewController, UITableViewDataSource
    

    This makes UITableViewDataSource a requirement for your ViewController. Next you need to set the datasource on your UITableView. So....

        override func viewDidLoad() {
            super.viewDidLoad()
            navigationItem.title = quake.place
    
            tableInfoQuake.datasource = self     
        }
    

    Lets say you want to now add 7 cells to the table, and all 7 will say "Hello Man". For this example, I am going to do it the worst yet simplest way. if you want me to go more in depth, just comment below, and I will do it the better way.

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 7
        }
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
            let cell = UITableViewCell()
            let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
            label.text = "Hello Man"
            cell.addSubview(label)
            return cell
        }
    

    So thats it for step one. We have told the table that the ViewController has what it is looking for in terms of data, and we have returned data for it to use.


    Step 2: Implement The Delegate

    Much like Step 1, this step starts with adding something to the top...

        // change top to...
    
        class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate
    

    Then, again, much like the top, we will edit the viewDidLoad() function...

        override func viewDidLoad() {
            super.viewDidLoad()
            navigationItem.title = quake.place
    
            tableInfoQuake.datasource = self
            tableInfoQuake.delegate = self
        }
    

    And now we must implement the delegate methods that designate the height of each cell.

        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            return 50
        }
    

    We have now added the delegate to the table, and implemented the method that tells the UITableView what the height of each cell is.


    Lets Put It All Together

        class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate {
    
            @IBOutlet var tableInfoQuake: UITableView!
    
            override func viewDidLoad() {
                super.viewDidLoad()
    
                tableInfoQuake.datasource = self
                tableInfoQuake.delegate = self
            }
    
    
            // UITableViewDataSource Functions
    
            func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return 7
            }
    
            func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
                let cell = UITableViewCell()
                let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
                label.text = "Hello Man"
                cell.addSubview(label)
                return cell
            }
    
    
            // UITableViewDelegate Functions
    
            func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
                return 50
            }        
        }
    

    Good luck to you.

    ZR

提交回复
热议问题