美文网首页
UITableView的问题汇总

UITableView的问题汇总

作者: YanZi_33 | 来源:发表于2022-01-20 10:05 被阅读0次

    UITableView样式Style为UITableViewStylePlain,当设置FooterView时,会出现FooterView的悬停

    • 解决方案一:依然采用UITableViewStylePlain的样式,不使用FooterView,以自定义cell来代替FooterView;
    • 解决方案二:采用UITableViewStyleGrouped样式,为指定的section设置FooterView时,tableView的头尾会出现默认高度可通过代理方法去除默认高度,注意⚠️高度不能设置为0(设置为0无效),可设置为0.01即可;
    • OC版本,代码如下:
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 0.01;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 0.01;
    }
    
    • Swift版本,代码如下:
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    

    UITableViewHeaderFooterView无法直接修改背景颜色

    • 解决方案:在UITableViewHeaderFooterView内部创建一个等尺寸大小的UIView,作为背景控件,可修改背景控件的颜色,从而达到修改UITableViewHeaderFooterView的背景颜色;
    • Swift版本,代码如下:
    import UIKit
    
    class SFBaseTableHeaderFooterView: UITableViewHeaderFooterView {
    
        override init(reuseIdentifier: String?) {
            super.init(reuseIdentifier: reuseIdentifier)
            createUI()
            layoutSubControls()
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        func createUI() -> Void {
            self.addSubview(self.backView)
        }
        
        func layoutSubControls() -> Void {
            self.backView.snp.makeConstraints { (make) in
                make.edges.equalTo(self)
            }
        }
        
        class func identifier() -> String {
            return String(describing: self)
        }
        
        //MARK: -- lazy
        //为了修改header footer的背景色 自定义背景view
        lazy var backView: UIView = {
            let backView = UIView()
            backView.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1.0)
            return backView
        }()
    }
    

    相关文章

      网友评论

          本文标题:UITableView的问题汇总

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