怎样使用系统的tableView

在这篇文章中我会记录下怎么使用系统提供的tableView实现简单的设置页面

var tableView :UITableView?//声明tableView
var listArr = ["企业编号","用户账号","手机号码","邮箱地址","修改密码","退出登录"]//定义一个列表的数组
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "账号安全"
        setupUI()
    }
    //页面初始化
    func setupUI() {
        self.view.backgroundColor = UIColor.white
        //tableView有两种style 一个是plain 一个是grouped
        tableView = UITableView(frame: self.view.frame, style: UITableViewStyle.plain)
        //设置代理
        tableView?.delegate = self
        tableView?.dataSource = self
        tableView?.backgroundColor = UIColor.init(valueRGB: 0xf0f2fa)
        tableView?.tableFooterView = UIView(frame: CGRect.zero)//这个很重要作用是把tableView没有用到的cell去掉
        self.view.addSubview(tableView!)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    //MARK:- TABLEVIEW代理方法
    //这里顺便说一下注释的其中一种方式     MAKE:- 这里是你要写的信息
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 2
        case 1:
            return 2
        case 2:
            return 1
        default:
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")//首先要声明 一个cell
        if (cell == nil) {//在这里完成了cell的复用
            cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
//这里的写法就等于以注册方式实现的复用 因为要用到系统的cell样式 所以就自己写复用
//tableView?.register(UITableViewCell.self, forCellReuseIdentifier: "cell") //当然这行要写在didload里
//var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        }//如果要使用detailLabel就不能用cellstyle.default,我这里使用的是value1 其他两种样式可以自己试下
        
        //根据组tag 显示不同的内容,系统提供的value1的cell样式有两个Label 一个是左边的textLabel 一个是右边的detailTextLabel
        switch indexPath.section {
        case 0:
            cell?.textLabel?.text = listArr[indexPath.row]
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 16)//设置字号
            cell?.detailTextLabel?.textAlignment = NSTextAlignment.right//设置字体对齐方式
            switch indexPath.row {
            case 0:
                cell?.detailTextLabel?.text = "hpqk"//右边Label显示的内容
            default:
                cell?.detailTextLabel?.text = "lihang"
            }
        case 1:
            cell?.textLabel?.text = listArr[indexPath.row+2]
            cell?.detailTextLabel?.textAlignment = NSTextAlignment.right
            cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator//这的意思是在cell上设置一个右三角
            switch indexPath.row {
            case 0:
                cell?.detailTextLabel?.text = "13263264031"
            default:
                cell?.detailTextLabel?.text = "hangliwill@163.com"
            }
        case 2:
            cell?.textLabel?.text = listArr[4]
            cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        default://因为最后一组我需要自定义所以在cell上添加了一个居中的Label
            let quitLB = UILabel(frame: CGRect(x: (SCREEN_WIDTH-80)/2, y: 13.35, width: 80, height: 16))
            quitLB.textColor = UIColor.red
            quitLB.textAlignment = NSTextAlignment.center
            quitLB.text = listArr[5]
            cell?.addSubview(quitLB)//记得一定要添加到cell上
            cell?.separatorInset = UIEdgeInsetsMake(0, 0, 0, (cell?.bounds.size.width)!*2)//这里是把最后一个cell的分割线挪出屏幕 spearatorinset的作用是设置cell分割线的位置
        }
        
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 42.7//返回的行高
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10//返回的组头高度
    }
    //自定义组头想要改变组头的颜色或加图标啊 什么的就需要自定义了
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        //首先你要自定义一个view
        let headerSctionView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 10))
        headerSctionView.backgroundColor = UIColor.init(valueRGB: 0xf0f2fa)
        //这里可以添加各种控件并把他们添加到你自定的view上,最后返回view就可以了
//        let icon = UILabel(frame: CGRect(x: <#T##CGFloat#>, y: <#T##CGFloat#>, width: <#T##CGFloat#>, height: <#T##CGFloat#>))
//        headerSctionView.addSubview(icon!)
        return headerSctionView
        
    }
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,812评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,305评论 4 61
  • 再过三天 即将离开深圳 当初 心之向往的城市,将带着这份憧憬飞往杭州。 ...
    穆清yvonne阅读 182评论 2 1
  • ​ 今天酷哥给各位带来的是: 一位不著名的画家兼设计师 波兰画家 Rafal Olbinski 的作品 他的作品被...
    画酷阅读 554评论 0 0
  • 昨天晚上老板开会,然后就开始了对抗模式。 早上司机师傅说,给我评个五星。我突然有种被命令的感觉,我不喜欢这种感觉,...
    艳敏姐阅读 125评论 0 0