补充笔记04-tableview的简单设置

tableview的简单设置

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var Name: [String]?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Name = ["切尔斯", "里斯", "威尔斯"]
        
        //列表铺满整个view
        let tableView = UITableView(frame: self.view.bounds, style: .Plain)
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(tableView)
        
        //设置tableview头的颜色
        let headView = UIView(frame: CGRect(x: 100, y: 0, width: 100, height: 100))
        headView.backgroundColor = UIColor.blueColor()
        tableView.tableHeaderView = headView
        
        //设置tableview尾的颜色
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 200))
        footerView.backgroundColor = UIColor.redColor()
        tableView.tableFooterView = footerView
    }
    
    //设置section的显示个数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Name!.count
    }
    
    //设置tableview的显示个数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 5
    }
    
    //设置选择点击section显示section的内容
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(indexPath.section, indexPath.row)
        print(Name![indexPath.row])
    }
    
    //设置section的头的颜色
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = UIColor.brownColor()
        return v
    }
    
    //设置section的头的高度
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    
    //设置section的尾的高度
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 45
    }
    
    //设置section的尾的颜色
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v1 = UIView()
        v1.backgroundColor = UIColor.blackColor()
        return v1
    }
    
    //设置tableview列表显示的内容
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            var cell = tableView.dequeueReusableCellWithIdentifier("cell")
            if cell == nil {
                cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
            }
        cell?.textLabel?.text = Name![indexPath.row]
        return cell!
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容