UITableView

初始化

lazy var tbvData: UITableView = {
        
        let tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.MJTheme.bg
        tableView.rowHeight = 70
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.tableFooterView = UIView.init(frame: .zero);

        let cell = UINib.init(nibName: "KSimpleTitleTbvCell", bundle: nil)
        tableView.register(cell, forCellReuseIdentifier: "KSimpleTitleTbvCell")
        return tableView
    }()

self.view.addSubview(tbvData)
        tbvData.snp.makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }

自定义cell,分区头部,尾部

---IB cell
let cell = UINib.init(nibName: "KSimpleTitleTbvCell", bundle: nil)
tableView.register(cell, forCellReuseIdentifier: "KSimpleTitleTbvCell")
let sectionHead = UINib.init(nibName: "WorkHomeTbvSectionHeadView", bundle: nil)
tableView.register(sectionHead, forHeaderFooterViewReuseIdentifier: "WorkHomeTbvSectionHeadView")
---class cell
tableView.register(MJTableViewCell.self, forCellReuseIdentifier: "MJTableViewCell")
tableView.register(MJSectionHeadView.self, forHeaderFooterViewReuseIdentifier: "MJSectionHeadView")
tableView.register(MJSectionFootView.self, forHeaderFooterViewReuseIdentifier: "MJSectionFootView")


 let cell = MJTableViewCell(style: .subtitle, reuseIdentifier: "MainClassicalCell")
    

表头,表尾

   lazy var tbvHeadView: WorkHomeTbvHeadView = {
        let tableHeadView = Bundle.main.loadNibNamed("WorkHomeTbvHeadView", owner: self, options: nil)?.last as! WorkHomeTbvHeadView
        tableHeadView.frame = CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 100)
        tableHeadView.autoresizingMask = UIViewAutoresizing(rawValue: 0)

        return tableHeadView
    }()
  lazy var tbvFootView: WorkHomeTbvHeadView = {
        let tableFootView = Bundle.main.loadNibNamed("WorkHomeTbvHeadView", owner: self, options: nil)?.last as! WorkHomeTbvHeadView
        tableFootView.frame = CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 100)
        tableFootView.autoresizingMask = UIViewAutoresizing(rawValue: 0)
        
        return tableFootView
    }()

        tableView.tableHeaderView = self.tbvHeadView
        tableView.tableFooterView = self.tbvFootView

常用代理方法

   extension NewHomeVC: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell( withIdentifier: "KSimpleTitleTbvCell", for: indexPath) as! KSimpleTitleTbvCell
        cell.labTitle.text = dataArray[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    
}

分区头部,尾部

    /*分区头部部高度*/
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let sectionHead = tableView.dequeueReusableHeaderFooterView(withIdentifier: "WorkHomeTbvSectionHeadView") as! WorkHomeTbvSectionHeadView
        sectionHead.labTitle.text = "华南名医联盟"
        return sectionHead
        
    }
    
    /*分区尾部高度*/
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
         let sectionFoot = tableView.dequeueReusableHeaderFooterView(withIdentifier: "WorkHomeTbvSectionHeadView") as! WorkHomeTbvSectionHeadView
        sectionFoot.labTitle.text = "华南名医联盟"
        return sectionFoot
    }

UITableView 方法,特性

`默认选中第一行`
let path = IndexPath(item: 0, section: 0)
tableView(tbvData, didSelectRowAt: path)
`一句话去除UITableView底部多余行及分割线`
tableView.tableFooterView = UIView(frame: CGRect.zero)
`取消分割线`
tableView.separatorStyle = .none
`分割线颜色`
tableView.separatorColor = UIColor.red
`右边尖括号`
cell.accessoryType = .disclosureIndicator
`去掉点击效果`
cell.selectionStyle = .none
`一个section刷新`
let indexSet = NSIndexSet(index: 2)
tbvData.reloadSections(indexSet as IndexSet, with: .automatic)
`一个cell刷新`
let indexPath = IndexPath(row: 3, section: 0)
tbvData.reloadRows(at: [indexPath], with: .none)
`滚动到指定位置 `
var dayOne = IndexPath(row: 0, section: 2)
tbvData.scrollToRow(at: dayOne, at: .top, animated: true)
`滚动到顶部`
tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
`cell 左右空隙`
override var frame: CGRect{
        didSet{
            var f: CGRect = frame
            f.origin.x = kTbvSpace
            f.size.width = frame.size.width - kTbvSpace * 2
            super.frame = f
        }
    }
`iOS 11 设配  滑动卡顿(漂移和抽疯的现象)3句不少 重要`
tableView.estimatedRowHeight = 0
tableView.estimatedSectionHeaderHeight = 0
tableView.estimatedSectionFooterHeight = 0
`点击松开灰色取消`
tableView.deselectRow(at: indexPath, animated: true)
`取消向下偏移 64/20`
if #available(iOS 11.0, *) {
            srvData.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
`禁止滑动`
tableView.isScrollEnabled = false
`分割线边距`
tableView.separatorInset = UIEdgeInsets(top: 0, left: 17, bottom: 0, right: 0)
`注意cell 约束的内边距`
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0)
`sectionView 有默认颜色,xib 不修改他的 颜色,添加一个view`
let sectionHead = UIView.init()
sectionHead.backgroundColor = tableView.backgroundColor
return sectionHead
`点击颜色修改`
cell.selectedBackgroundView = UIView(frame: cell.frame)
cell.selectedBackgroundView?.backgroundColor = kBgColor
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容