zoukankan      html  css  js  c++  java
  • 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!

        

        }

  • 相关阅读:
    wenbao与分治
    wenbao与贪心
    wenbao与高精度
    wenbao与FFT
    wenbao与卡特兰数
    wenbao与类
    wenbao与取整函数
    wenbao与字符串转化
    wenbao与scanf
    wenbao与数字与字符串
  • 原文地址:https://www.cnblogs.com/jiang-xiao-yan/p/6385761.html
Copyright © 2011-2022 走看看