Swift-表格

Posted 姜晓延

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift-表格相关的知识,希望对你有一定的参考价值。

 //1.创建表格

        let tv = UITableView(frame: view.bounds, style: .Plain)

        

        //2.添加到视图

        view.addSubview(tv)

        

        //3.注册可以使用的cell  [UITableViewCell class] OC

         tv.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellID")

        //4.设置数据源

        //Swift 中没有遵守协议是一个错误

        tv.dataSource = self

       (遵守协议 UITableViewDataSource)

  注:在Swift中没有 @interface ViewController (),因此在  class ViewController: UIViewController , UITableViewDataSource

实现协议方法:

 //MARK:UITableViewDataSource

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 20

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cellID")

        //提示:textLabel 是可选的

        //代码中?是自动带的,如果textLabel有就使用

        cell?.textLabel?.text = "hello world ----\(indexPath.row)"

        return cell!

    

    }

 

以上是关于Swift-表格的主要内容,如果未能解决你的问题,请参考以下文章