使用Playground‘玩’界面 - 2

这一篇主要说一下如何在Playground中使用ViewController。

首先我们创建一个DemoTableViewController。

public class DemoTableViewController : UIViewController,UITableViewDataSource {
    lazy var tableView:UITableView = UITableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
    override public func viewDidLoad() {
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        view.addSubview(tableView)
        self.tableView.frame = self.view.frame
    }
   
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.text = String(indexPath.count)
        return cell
    }
}

在这个ViewController中我添加了一个TableView。然后再使用使用Playground‘玩’界面 - 1中提到的PlaygroundPage.current.liveView就可以显示这个ViewController了。

PlaygroundPage.current.liveView = DemoTableViewController.init()
效果图

完整代码如下:

import UIKit
import PlaygroundSupport

public class DemoTableViewController : UIViewController,UITableViewDataSource {
    lazy var tableView:UITableView = UITableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
    override public func viewDidLoad() {
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        view.addSubview(tableView)
        self.tableView.frame = self.view.frame
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.text = String(indexPath.item)
        return cell
    }
}

PlaygroundPage.current.liveView = DemoTableViewController.init()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 每日必读:12条人生原则 1. 早睡早起——晚不超过23:30,早不晚于5:00,醒后不睡回笼觉。 2. 谨言——...
    衡山阅读 379评论 2 3
  • 系列阅读 Java基础:类加载器 Java基础:反射 Java基础:注解 Java基础:动态代理 1. 概述 注解...
    JackChen1024阅读 559评论 0 2
  • 窑湾,于我只是个新名词,来之前从未听说过,工作的地方因为与江苏搭界,江苏的一些地名略有耳闻,明显有别于山东,或许水...
    心若芷兰阅读 525评论 4 16
  • 自从前几周读完了《我的天才女友》,心里一直记挂着书中两位主角的命运,于是这两周就给自己定下了目标,一定要读完这个系...
    谷雨写在四月后阅读 1,816评论 0 6