美文网首页iOS程序猿
IOS:Swift-UITableView

IOS:Swift-UITableView

作者: 任任任任师艳 | 来源:发表于2016-11-29 08:21 被阅读0次

    AppDelegate:
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController = ViewController()
        
        return true
    }
    

    ViewController.swift:
    /*
    UITableView的父类时UIScrollView,但是UITableView只能上下滚动,UITableView通过UITableCell(或者是自定义的cell)展示数据,
    UITAbleCell上有一个contentView、contentView上有一个titleLabel,detitleLabel,imageView,accssoryView(辅助视图),且UITableView可以进入编辑模式,能够增删改
    2.UITableView可以有0个或多个分区,分区是根据分区的下标来区分的,没个分区有多个(rows)cell,cell也是根据所在分区的下标来区分
    3.UITableView有两种样式plain和grouped,而且一旦确定就不能修改
    4.YUITableView很多方法都和NSindexPath对象有关,要么作为方法的参数,要么作为方法的返回值

    */
    //二、UITableView显示相关属性
    //三/代理:datasource显示数据相关的代理 delegate 业务相关的代理
    import UIKit

    //屏幕的宽
    let kScreenWidth = UIScreen.main.bounds.size.width
    //屏幕的高
    let kScreenHeigh = UIScreen.main.bounds.size.height

    class ViewController: UIViewController,UITableViewDataSource {
    //定义一个tableVeiw的属性-替换属性 强转
    var tableView:UITableView!

    //重写loadView方法修改视图控制器自带的view,将tableView作为视图控制器自带的view
    //防崩有两种方法1.super调用父类。2.将自定义视图tableview赋值给view
    override func loadView() {
        //创建UITableView
         tableView = UITableView(frame:
            UIScreen.main.bounds, style: .grouped)
        
        self.view = tableView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //配置tableView的常用属性
        //rowHeigh 行高
         tableView.rowHeight = 100
        //分割线样式  separatorStyle
        tableView.separatorStyle = .singleLine
          // 分割线内边距属性
        tableView.separatorInset = UIEdgeInsets(top: 0 , left: 0, bottom: 0, right: 0)
        //分割线的颜色
        tableView.separatorColor = UIColor.red
        //tableHeaderView UITableView的置顶视图   UITableView的表头视图经常来放置轮廓图
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 150))
        headerView.backgroundColor = UIColor.green
        
        tableView.tableHeaderView = headerView
        
        
        //UITableView的表尾视图,经常用来收起多余的cell
    

    // let footerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 20))
    // footerView.backgroundColor = UIColor.cyan
    // tableView.tableFooterView = footerView

        //快速收起多余的cell
        tableView.tableFooterView = UIView()
        
        //tableView的重要属性,datasource,显示数据源代理,专门负责tableView数据显示,可以控制tableView有多少分区,没个分区有多少cell。以及返回UITableViewCell控件
        tableView.dataSource = self
        
        
    }
    
    实现方法:
    //MARK:-UITableViewDataSoure协议中的方法
        //返回每个分区中cell的个数
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
            //存储的事分区的下标
            return section % 2 == 0 ? 1 : 3
        
        }
        //返回TableViewcell的方法
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        //创建cell对象
            //参数1:cell的样式
            //重用标识
            let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
            //给属性赋值
    //        cell上有一个contentView,contentView上有textlabel、detaillabel、iamgeView
        cell.textLabel?.text = "text"
            cell.detailTextLabel?.text = "detail"
            cell.imageView?.image = #imageLiteral(resourceName: "a02.jpg")
    //        cell.contentView 在cellView上imageView等都在其上显示
                      //style的样式
            //.default无detail
            //.subtitle image  detile.title 上下
            //.value1  image    detile.title左右
            //.value2   无image
            
                 //系统提供的accessType样式  辅助视图
    //        cell.accessoryType = .detailButton             !   里面有东西可以点
    //         cell.accessoryType = .disclosureIndicator      >   里面有东西可以点
            //赋值UIControl 子类作为辅助视图
    //        cell.accessoryView = UISwitch()
            
            //自定义视图作为辅助视图
            //以后,如果我们要自定义cell也要吧自定义控件加到contentView上
            let accView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
            accView.backgroundColor = UIColor.blue
            cell.accessoryView = accView
            
            //
            
            return cell
        }
        //MARK:-返回tableView有多少分区  默认不设置这个方法。有一个分区,但是分区的下标是0
        func numberOfSections(in tableView: UITableView) -> Int {
            return 5
        }
        //返回分区区头的标题
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            //section存储的也是分区的下标
            return "区头标题\(section)"
        }
        //返回分区尾部(区尾)的标题
        func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
            return "区尾标题\(section)"
        }
        
        //返回右边侧边索引的方法
        func sectionIndexTitles(for tableView: UITableView) -> [String]? {
            return ["0","1","2","3","4","5"]
        }
    
    

    相关文章

      网友评论

        本文标题:IOS:Swift-UITableView

        本文链接:https://www.haomeiwen.com/subject/zsdapttx.html