zoukankan      html  css  js  c++  java
  • TableView与delegate、dataResource

    小梦这几天学习tableView是深有体会了

    废话不多说,来一波

    首先,创建一个测试项目

    如图

    创建好,在项目结构中另外弄一个GroupFile,创建storyBoard和CocoaTouch

    在storyBoard里面放一个普通的tableView控件

    给这个storyBoard做好准备工作{

      关联一个CocoaTouch类,

      并且设置这个storyBoard为第一个场景

    }

    接下来就开始在CocoaTouch类里写代码来操作storyBoard里的tableView

    Swift代码:

    import UIKit
    
    class tableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
        //来一个数组
        var stu = ["123":["xx","xxx","xxx"],"124":["xxx","xxx","xxx"],"125":["xxx","xxx","xxx"]].sorted(by: {$0.key < $1.key})
        
        //返回节的数量,这个是委派里的可选方法
        func numberOfSections(in tableView: UITableView) -> Int {
            return stu.count
        }
        
        //返回节的名称
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return stu[section].key
        }
        
        //返回每个节下数据的条数
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return stu[section].value.count
        }
        
        //所有数据
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            //弄一个cellid单元格标识属性
            let cellid = "cellid"
            
            //拿到单元格对象,通过此方法的参数tableView来获取
            var cell = tableView.dequeueReusableCell(withIdentifier: cellid)
            
            //判断是否为空
            if cell == nil {
                //若为空,就给一个默认样式,不显示任何东西
                cell = UITableViewCell(style: .default, reuseIdentifier: cellid)
            }
            
            //反之就开始显示所有数据
            cell?.textLabel?.text = stu[indexPath.section].value[indexPath.row]
            
            //返回cell
            return cell!
        }
    }
    

    上面代码里,所有的方法都有注释解析 

    代码写好了,先别那么急

    还有一步

    在StoryBoard的树结构里,讲tableView关联好dataSource和delegate后

    运行

    完美实现,是不是很简单呢,当然,多加练习,谁都能从小白立马转大神级别

     ^_^

    Thank

    --------------------------------------------------Over

  • 相关阅读:
    IIS5布署MVC3
    操作数数据类型 ntext 对于 max 运算符无效
    iis配置
    IIS取消目录浏览
    mediamind SyncAds
    告诉你如何做网站运营
    xml as3解析
    mediamind 组件
    AIR for androd 笔记
    开发AIR for android 大象游戏合集开发总结
  • 原文地址:https://www.cnblogs.com/LiJinfu/p/6146743.html
Copyright © 2011-2022 走看看