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
}
}

最新更新