[求助]iOS调用reloadData()不刷新数据,不调用cellForRowAt方法

代码如下:主线程调用了tableView的reloadData()之后只调用了numberOfRowsInSection,没有调用cellForRowAt一直找不到解决的方法还有出错的原因,希望有大神知道原因的能指点一下,感激不尽


import UIKit

protocol SearchViewDelegate {
    ///点击排序按钮调用
    func searchView(clickSortBtn btn: UIButton)
    ///选中商品调用
    func searchView(clickShopCell indexPath: IndexPath)
}

class SearchView: UIView, UITableViewDataSource, UITableViewDelegate {

    ///代理属性
    var chDelegate: SearchViewDelegate?

    var snapBtn = UIButton()
    let btnTitles = ["相关", "销量", "价格", "新品"]
    var bottomLine = UIView()
    var shopTableV = UITableView()
    ///模型数组
    var models: [SearchShopResultModel] = []


    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.chBackGroundColor
        self.shopTableV.register(SearchViewShopCell.self, forCellReuseIdentifier: "searchShopCell")
        shopTableV.dataSource = self
        shopTableV.delegate = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///布局子视图
    override func layoutSubviews() {
        ///1. 排序分类按钮
        let btnBackV = UIView()
        btnBackV.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 40)
        btnBackV.backgroundColor = UIColor.white
        self.addSubview(btnBackV)
        for i in 0...count {
            let btn = UIButton.init(type: .custom)
            btn.setTitle(btnTitles[i], for: .normal)
            btn.setTitle(btnTitles[i], for: .highlighted)
            btn.setTitleColor(UIColor.gray, for: .normal)
            btn.setTitleColor(UIColor.red, for: .highlighted)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
            btn.addTarget(self, action: #selector(typeBtnClick(btn:)), for: .touchUpInside)
            ///frame
            let btnW: CGFloat = self.frame.width / CGFloat(btnTitles.count)
            let btnH: CGFloat = btnBackV.frame.height
            let btnX: CGFloat = btnW * CGFloat(i)
            let btnY: CGFloat = btnBackV.frame.minY
            btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            btnBackV.addSubview(btn)
            if i == 0 {
                ///添加下划线
                let bottomLine = UIView()
                bottomLine.backgroundColor = UIColor.red
                let lineX: CGFloat = btn.frame.minX + 10
                let lineY: CGFloat = btn.frame.maxY - 2
                let lineW: CGFloat = btn.frame.width - 20
                let lineH: CGFloat = 2
                bottomLine.frame = CGRect(x: lineX, y: lineY, width: lineW, height: lineH)
                btnBackV.addSubview(bottomLine)
                self.bottomLine = bottomLine
                ///发送点击事件
                typeBtnClick(btn: btn)
            }
        }

        ///2. 搜索结果列表
        let tableY: CGFloat = btnBackV.frame.maxY + 10
        let tableX: CGFloat = 0
        let tableW: CGFloat = UIScreen.screenWidth
        let tableH: CGFloat = UIScreen.screenHeight - tableY
        self.shopTableV.frame = CGRect(x: tableX, y: tableY, width: tableW, height: tableH)
        self.addSubview(shopTableV)
    }


    //MARK: - UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = SearchViewShopCell.cellWithTable(tableView: tableView)
        let model = models[indexPath.row]
        cell.detailText = model.goodsName
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    //MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("点击了\(indexPath.row)")
    }

    ///点击事件
    func typeBtnClick(btn: UIButton) {
        snapBtn.setTitleColor(UIColor.gray, for: .normal)
        btn.setTitleColor(UIColor.red, for: .normal)
        snapBtn = btn
        ///添加下划线
        bottomLine.backgroundColor = UIColor.red
        let lineX: CGFloat = btn.frame.minX + 10
        let lineY: CGFloat = btn.frame.maxY - 2
        let lineW: CGFloat = btn.frame.width - 20
        let lineH: CGFloat = 2
        UIView.animate(withDuration: 0.2) { 
            self.bottomLine.frame = CGRect(x: lineX, y: lineY, width: lineW, height: lineH)
        }
    }

    //MARK: - 外部方法
    public func reloadView() {
        DispatchQueue.main.async {
            //刷新表格数据
            self.shopTableV.reloadData()
        }
    }
}

你可能感兴趣的:([求助]iOS调用reloadData()不刷新数据,不调用cellForRowAt方法)