Swift --**表格书写**

Swift –表格书写

第一步定义表格 这里 swift的初始化与oc不太一样 但是思想是一样的

全局变量
var tableView:UITableView?
//设置表格的位置大小 代理 重中之重就是表格的代理协议

<UITableViewDelegate,UITableViewDataSource>

self.tableView=UITableView(frame: self.view.frame,style:.grouped )
//代理
tableView?.delegate=self
tableView?.dataSource=self
//添加表格
self.view .addSubview(self.tableView!)

    //创建一个重用的单元格  //注册
    self.tableView!.register(UITableViewCell.self,
                             forCellReuseIdentifier: "SwiftCell")

单元格方法中的 与上边注册迎合复用

//单元格返回行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

}

//单元格内容书写方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//为了提供表格显示性能,已创建完成的单元需重复使用
let identify:String = “SwiftCell”
//同一形式的单元格重复使用,在声明时已注册
let cell = tableView.dequeueReusableCell(withIdentifier: identify, for: indexPath)
}

猜你喜欢

转载自blog.csdn.net/qq_22049111/article/details/84141961