zoukankan      html  css  js  c++  java
  • SWIFT UITableView的基本用法

    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            // Override point for customization after application launch.
            self.window!.backgroundColor = UIColor.whiteColor()
            
            let navigation = UINavigationController(rootViewController: RootViewController())
            self.window?.rootViewController = navigation
            
            self.window!.makeKeyAndVisible()
            return true
        }
    
    }
    import UIKit
    
    class RootViewController: UIViewController {
    
        override func loadView() {
            super.loadView()
            //初始化UITableView
            let tableView = UITableView()
            tableView.frame = UIScreen.mainScreen().bounds
            tableView.dataSource = self
            tableView.delegate = self
            self.view.addSubview(tableView)
        }
        //懒加载数据
        lazy var datas:[String] = {
            return ["是雨是泪","分分钟需要你","伤心太平洋","曾经最痛","飘雪"]
        }()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.title = "UITableView的基本用法"
        }
    }
    
    extension RootViewController:UITableViewDelegate,UITableViewDataSource
    {
        //区的个数
        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
        //在相应区中cell的个数
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return datas.count
        }
        // cell的高度
        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            return 60
        }
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            //在重用机制里取出cell
            var cell = tableView.dequeueReusableCellWithIdentifier("cell")
            //如果cell为空则创建
            if cell == nil {
                cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
            }
            //设置数据
            cell?.textLabel?.text = datas[indexPath.row]
            return cell!
        }
        
    }
  • 相关阅读:
    Windows上git输错一次密码不在提示输入密码
    JSON Web Token 简介
    Springboot+Shiro+Jwt实现权限管理
    Springboot配置外部容器使用JNDI读取数据源
    Springboot解决Main方法启动无法注入JNDI
    Springboot2.1.6版本部署resin4.0.62
    Java解决多线程无法@Autowired注入,手动获取Bean对象
    Linux设置Vim显示行号
    Linux使用wget后台下载
    排查生产环境CPU过高的问题
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5208296.html
Copyright © 2011-2022 走看看