zoukankan      html  css  js  c++  java
  • IOS的UITableView控件简单使用

    在IOS组件中,UITableView是几乎每个应用都会使用到的控件,没有之一。

    UITableView简单使用

     var arr : [String]?
    
     override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            self.arr = ["1","2","3"]
            
            let tableView = UITableView(frame: self.view.bounds,style: UITableView.Style.plain)
            tableView.dataSource = self
            tableView.delegate = self;
            tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "qingcheng")
            self.view.addSubview(tableView)
        }
    
    

    然后实现 UITableViewDelegate,UITableViewDataSource 两个协议。

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            print("执行总条数")
            return arr!.count;
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "qingcheng",for: indexPath)
            cell.textLabel?.text = "test"
            return cell
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 100;
        }
    

    点击运行

    自定义cell

    复杂一点的应用都会自定义一个cell来满足自己的设计需求

    使用xib来实现

    创建一个cocoa touch class,勾选xib

    在ViewDidLoad的时候,可以需要使用UINib来加载Xib

     let tableView = UITableView(frame: self.view.bounds,style: UITableView.Style.plain)
            tableView.dataSource = self
            tableView.delegate = self;
            tableView.register(UINib.init(nibName: "MyCellTableViewCell", bundle: nil), forCellReuseIdentifier: "qingcheng")
            self.view.addSubview(tableView)
    

    协议实现加载cell的方法也需要做对应的调整

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell:MyCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "qingcheng",for: indexPath) as! MyCellTableViewCell
            cell.setName(name: "青城同学")
            return cell
        }
    

    然后点击运行。

    简单的使用差不多就到这里了。UITableView是一个非常强大的控件,比如删除cell,cell排序,机会都可以几行代码实现。

    欢迎关注我的微信搜索公众号 【青城同学】,不定时和你分享一些技术和有趣的事情

  • 相关阅读:
    开源云平台离普通用户还有多远?
    你的云桌面和阿里的云主机有什么区别?
    容器和虚拟机谁会是未来云计算的方向?
    白话为什么需要虚拟机和云计算有什么关系
    OpenStack是什么?
    Kubernetes是什么?
    第二夜:万圣节,讲一个关于程序员的故事
    万圣节,讲一个关于程序员的故事
    云计算社区新增两枚 .group 社群专属域名
    正式激活 .group 域名:云原生.社群
  • 原文地址:https://www.cnblogs.com/boxrice/p/14766637.html
Copyright © 2011-2022 走看看