代码创建TableView 参考 : http://mft.iteye.com/blog/2314641
创建局部显示 或 自定义 容器 的 TableView
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { let screenWidth = UIScreen.main.bounds.width; let screenHeight = UIScreen.main.bounds.height; // var myTable:UITableView!; var baby = ["数据0","数据1","数据2","数据3","数据4","数据5","数据6","数据7","数据8","数据9","数据10","数据11"]; override func viewDidLoad() { super.viewDidLoad() myTable = UITableView(frame: CGRect(x: 10, y: 80, width: 100, height: 300)); myTable.delegate = self myTable.dataSource = self; myTable.layer.masksToBounds = true; myTable.layer.borderWidth = 2; myTable.layer.borderColor = UIColor.red.cgColor; self.view.addSubview(myTable); } //每行有几个区块 func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } //多少行 func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return baby.count } // 开始往每行写内容 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //--- var cell = tableView.dequeueReusableCell(withIdentifier: "diy_cell"); if(cell == nil){//因为是纯代码实现,没有对行里的cell做注册,这里是 做注册, 注册一次后,下次会继续使用这个缓存 cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "diy_cell"); //以上使用了系统默认的一个cell样式 } cell?.textLabel?.text = baby[indexPath.row]; return cell! //==== } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }