Creating a UITableView Programmatically

后端 未结 9 2077
梦谈多话
梦谈多话 2021-01-31 14:26

I have an application in Xcode 4.6 which uses storyboards. I added a UITableView to a view controller class, which worked as expected. However, when I tried deleting the UITable

9条回答
  •  有刺的猬
    2021-01-31 15:02

    Creating a tableview using tableViewController .

    import UIKit
    
    class TableViewController: UITableViewController
    {
        let tableViewModel = TableViewModel()
        var product: [String] = []
        var price: [String] = []
        override func viewDidLoad()
        {
            super.viewDidLoad()
            self.tableView.contentInset = UIEdgeInsetsMake( 20, 20 , 0, 0)
            let priceProductDetails = tableViewModel.dataProvider()
    
            for (key, value) in priceProductDetails
            {
                product.append(key)
                price.append(value)
            }
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {
            return product.count
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {
            let cell = UITableViewCell(style: .Value1, reuseIdentifier: "UITableViewCell")
            cell.textLabel?.text = product[indexPath.row]
            cell.detailTextLabel?.text = price[indexPath.row]
            return cell
        }
    
        override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
        {
            print("You tapped cell number \(indexPath.row).")
        }
    }
    

提交回复
热议问题