zoukankan      html  css  js  c++  java
  • Swift自定义头视图和尾视图

    var data: [[String]]!

        

        override func viewDidLoad() {

            super.viewDidLoad()

            // Do any additional setup after loading the view, typically from a nib.

            

            let tableView = UITableView(frame: self.view.bounds, style: .Plain)

            tableView.dataSource = self

            tableView.delegate = self

            self.view.addSubview(tableView)

            

            //数据源

            let resourcePath = NSBundle.mainBundle().pathForResource("font", ofType: "plist")

            self.data = NSArray(contentsOfFile: resourcePath!)! as! [[String]]

            

        }

        

        func numberOfSectionsInTableView(tableView: UITableView) -> Int {

            return self.data.count

        }

        

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return self.data[section].count

        }

        

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let identifier = "cell"

            var cell = tableView.dequeueReusableCellWithIdentifier(identifier)

            if cell == nil {

                cell = UITableViewCell(style: .Default, reuseIdentifier: identifier)

            }

            

            cell?.textLabel?.text = self.data[indexPath.section][indexPath.row]

            

            return cell!

            

        }

        

        //section的头视图标题

    //    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    //        return "第(section)个组的头视图"

    //    }

    //    

    //    //section的尾视图标题

    //    func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {

    //        return "第(section)个组的尾视图"

    //    }

        //自定义头视图

        func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

            //让headerView作为组的头视图,不需要设置headerView的frame,但是一定要指定高度

            let headerView = UIView(frame: CGRectZero)

            headerView.backgroundColor = UIColor.grayColor()

            let titleLabel = UILabel(frame: CGRect(x: 130, y: 10, 150, height: 20))

            titleLabel.font = UIFont.boldSystemFontOfSize(16)

            titleLabel.textColor = UIColor.greenColor()

            titleLabel.text = "第(section)个组的头视图"

            headerView.addSubview(titleLabel)

            

            return headerView

            

        }

        

        //自定义尾部视图

        func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

            let footerView = UIView(frame: CGRectZero)

            footerView.backgroundColor = UIColor.darkGrayColor()

            let titleLabel = UILabel(frame: CGRect(x: 130, y: 40, 150, height: 20))

            titleLabel.font = UIFont.boldSystemFontOfSize(16)

            titleLabel.textColor = UIColor.redColor()

            titleLabel.text = "第(section)个组的尾视图"

            footerView.addSubview(titleLabel)

            

            return footerView

        }

        

        //指定组的尾部视图的高度

        func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

            return 100

        }

        

        //指定组的头视图的高度

        func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

            return 50

        }

  • 相关阅读:
    Debian常用操作
    debian-11.0.0-amd64
    CentOS-8.4.2105-x86_64
    第20章: 二进制方式部署K8S1.20高可用集群
    天狮集团云函数实践:自定义业务逻辑实现跨境电商全球直播
    峰值利用率80%+,视频云离线转码自研上云TKE实践
    腾讯首个CNCF沙箱开源项目
    斗鱼直播云原生实践之注册中心篇
    Superedge的新特性和未来之路
    腾讯云与 Grafana Labs 达成深度合作, 推出全新 Grafana 托管服务
  • 原文地址:https://www.cnblogs.com/ZGSmile/p/5726063.html
Copyright © 2011-2022 走看看