Tableview simple display value

后端 未结 1 1610
执笔经年
执笔经年 2020-12-22 06:08

I am a stduent and very beginner of swift3. In here, I just want to display \"First\",\"Second\",\"Third\" in the tableview and it does not look good.

The tutorial f

相关标签:
1条回答
  • 2020-12-22 06:41

    Try This code: Code tested in Swift 3.

    Note: As you mentioned you just started programming Swift 3.I created a tableView programmatically. Just,copy below code and paste in to your viewController.run the project...

        import UIKit
    
        class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        private let MyArray: NSArray = ["First","Second","Third"]
        private var myTableView: UITableView!
    
        override func viewDidLoad() {
        super.viewDidLoad()
    
        // Status Bar 
        let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height 
    
        // View 
        let displayWidth: CGFloat = self.view.frame.width
        let displayHeight: CGFloat = self.view.frame.height
    
        // TableView 
        myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
    
        // Cell 
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
    
        // DataSource 
        myTableView.dataSource = self
    
        // Delegate 
        myTableView.delegate = self
    
        // View 
        self.view.addSubview(myTableView)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Num: \(indexPath.row)")
        print("Value: \(MyArray[indexPath.row])")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return MyArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) 
    
        cell.textLabel!.text = "\(MyArray[indexPath.row])"
    
        return cell
     }
    
    }
    

    Output:

    0 讨论(0)
提交回复
热议问题