swift4.0-tableView显示数据

2018-03-15  本文已影响70人  落夏简叶

用tableView很简单的显示数据,主要是熟悉API

代码:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tableView = UITableView(frame: view.bounds, style: .plain)
        tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "identifier")  //classForCoder获取类型
        tableView.dataSource = self
        view.addSubview(tableView)
        
    }
}

// MARK: UITableViewDataSource
extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 3
        case 1:
            return 5
        case 2:
            return 7
        default:
            return 0
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath)
        cell.textLabel?.text = "Section \(indexPath.section), Cell \(indexPath.row)"
        
        return cell
        
    }
}

上一篇 下一篇

猜你喜欢

热点阅读