微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift tableView.reloadData() 不会在 Swift 中重新加载表视图

如何解决Swift tableView.reloadData() 不会在 Swift 中重新加载表视图

尝试在获取请求后重新加载我的 tableView,但是,它不起作用。 我在第一个 tableViewCell 中有嵌入 collectionView 的 tableView。 这是我的 collectionView 类的样子:

class RegionCell: UITableViewCell,FetchRegionsDelegate {
    
    static let reuseId = "regionCellID"
    var collectionView: UICollectionView!
    var tableViewManager = RegionsAndCountriesTableVC()
    var selectedRegion: String?

    override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
        super.init(style: style,reuseIdentifier: reuseIdentifier)
        configureCollectionView()
        tableViewManager.delegate = self
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configureCollectionView() {
        collectionView = UICollectionView(frame: contentView.bounds,collectionViewLayout: createThreeColumnFlowLayout(in: contentView))
        contentView.addSubview(collectionView)
        collectionView.fillSuperview()
        collectionView.delegate   = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .systemBackground
        collectionView.register(RegionCollectionViewCell.self,forCellWithReuseIdentifier: RegionCollectionViewCell.reuseId)
    }
    
    
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        6
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RegionCollectionViewCell.reuseId,for: indexPath) as! RegionCollectionViewCell
        let region = regions[indexPath.item]
        cell.imageView.image = region.regionImage
        cell.textLabel.text = region.regionName
        if indexPath.row == 0 {
            collectionView.selectItem(at: indexPath,animated: true,scrollPosition: .left)
            cell.isSelected = true
        }
        return cell
    }

    
    func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
        tableViewManager.fetchByRegions(regionName: (regions[indexPath.item].regionName).lowercased())
            self.tableViewManager.tableView.reloadData()
    }

当我选择 didSelectItemAt 时,委托工作并且在 UITableViewController 中执行了一个带有新 API 调用函数,但是,即使 API 调用成功,tableView 单元格保持不变,即使在 self.tableView.reloadData() 之后叫做。这是我的 UITableViewController 类的外观:

protocol FetchRegionsDelegate {
    var selectedRegion: String? { get set }
}


class RegionsAndCountriesTableVC: UITableViewController {
    
    var countries = [Country]()
    var delegate: FetchRegionsDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(CountryCell.self,forCellReuseIdentifier: CountryCell.reuseId)
        tableView.register(RegionCell.self,forCellReuseIdentifier: RegionCell.reuseId)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        fetchInitialData()
    }


    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        section == 0 ? 1 : countries.count
    }

    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: RegionCell.reuseId,for: indexPath)
            return cell as! RegionCell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: CountryCell.reuseId,for: indexPath) as! CountryCell
            cell.textLabel?.text = countries[indexPath.row].name
            return cell
        }
    }
    
    override func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
        section == 0 ?  "Regions" : "Country"
    }
    
    override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        indexPath.section == 0 ? 250 : 45
    }

    // MARK: - FetchRequest
    
    func fetchInitialData() {
        let url = "https://restcountries-v1.p.rapidapi.com/all/?rapidapi-key=APIKey"
        fetchGenericJSONData(urlString: url) { (countries: [Country]?,error) in
            guard let safeCountries = countries else { return }
            self.countries = safeCountries
            dispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }
    
    func fetchByRegions(regionName: String) {
        if regionName == "all" {
            countries.removeAll()
            fetchInitialData()
        } else {
            countries.removeAll()
            print(self.countries.count)
            let url = "https://restcountries-v1.p.rapidapi.com/region/\(regionName)/?rapidapi-key=APIKey"
            fetchGenericJSONData(urlString: url) { (countries: [Country]?,error) in
                guard let safeCountriesByRegion = countries else { return }
                
                self.countries = safeCountriesByRegion
                print(self.countries.count)
                print(self.countries)
                dispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }

}

谁能告诉我我在这里做错了什么? 提前致谢!

解决方法

在表视图单元格中创建与父视图控制器无关的 RegionsAndCountriesTableVC 新实例不是您想要的,也是问题的原因。

您需要对父控制器的实际引用。

这可以通过回调闭包非常简单地完成。


RegionCell中定义一个回调属性,它传递区域名称

class RegionCell: UITableViewCell{

   var callback : ((String) -> Void)?
...

并调用它而不是委托

 func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
    let regionName = regions[indexPath.item].regionName.lowercased()
    callback?(regionName)
 }

删除与协议/委托相关的全部代码。


RegionsAndCountriesTableVC 中分配 cellForRow 中的回调

override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: RegionCell.reuseId,for: indexPath) as! RegionCell
        cell.callback = { regionName in
            self.fetchByRegions(regionName: regionName)
            tableView.reloadData()
        }
        return cell 
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: CountryCell.reuseId,for: indexPath) as! CountryCell
        cell.textLabel?.text = countries[indexPath.row].name
        return cell
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。