swift - TableView 中的 collectionView reloaddata
问题描述
我需要重新加载数据 collectionView。因为我在函数中追加到数组。我需要为此重新加载数据,但我收到错误“表达式类型不明确,没有更多上下文”。似乎没有添加数据,因为我没有重新加载数据。我如何使用 collectionView.reloadData?如何在 TableView 和 CollectionView 上重新加载数据?
class denemeView: UIViewController, UITableViewDataSource, UITableViewDelegate {
var davetiyeKategori = [String]()
var davetiyeKatIsım = [String]()
var storedOffsets = [Int: CGFloat]()
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return kategoriIsımYeni[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return kategoriIsımYeni.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? CategoryRow else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? CategoryRow else { return }
storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
}
func collectionReloadData(){
DispatchQueue.main.async(execute: {
self.collectionView.reloadData()
})
}
override func viewDidLoad() {
super.viewDidLoad()
davetiyeKategoriBul()
}
@objc func davetiyeKategoriBul(){
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["ISIM"] as? String {
let s = String(describing: soru_baslik)
self.kategoriIsımYeni.append(s)
DispatchQueue.main.async {
// self.tableViewKategoriler.reloadData()
}} }}
extension denemeView: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model[collectionView.tag].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! anaSayfaCell
let rowValue = model[collectionView.tag][indexPath.item]
print("urlNew", rowValue)
let urlNew = URL(string: (rowValue))
cell.denemeImage.sd_setImage(with: urlNew)
// cell.backgroundColor = model[collectionView.tag][indexPath.item]
print("model[collectionView.tag][indexPath.item]", model[collectionView.tag][indexPath.item])
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
}
}
解决方案
推荐阅读
- javascript - 代码审查:在 Javascript 中简洁地深度过滤对象数组
- function - PowerShell 函数。Foreach + If 查询、正确顺序或更多
- django - Django HTML 电子邮件消息自动从表单中选择接收者的电子邮件
- firefox - 没有后台同步事件的后台同步重播
- php - 使用 symfony 3.4 登录后如何更改 phpsessid
- java - 如何从 Java portlet (JSR-286) 中的字节数组下载 pdf 文件?
- css - 单击时更改 td 的颜色
- laravel - Laravel 验证器只允许一个字段而不是两个字段
- amazon-web-services - api网关中的2个api如何共享相同的lambda函数但不同的环境变量
- parsing - 有没有一种聪明的方法可以在 ANTLR 中使用不同类型的括号