我有一个 UITableView Controller 类,里面有一个 String 数组。 TableView 有 1 个单元格原型(prototype),其中有一个带有 1 个单元格原型(prototype)的 UICollectionView。
通过将数组传递到 TableView 单元格来填充 CollectionView,它是一个 UICollectionView 委托(delegate)和数据源。
当我更改 TableViewController 内的数组并调用 self.tableView.reloadData() 时,单元格内的 CollectionView 不会更新。
我该如何解决这个问题?
谢谢
编辑:
TableViewController 中的代码:
@IBAction func addToArrayAction(sender: AnyObject) {
self.testArray.append("Ant-Man")
self.tableView.reloadData()
}
var testArray = ["Guardinas", "Iron Man", "Avengers", "Captain America"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("collectionContainerCell", forIndexPath: indexPath) as! TableViewCell
cell.testArray = self.testArray
return cell
}
UITableViewCell 中的代码:
var testArray: [String]?
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
cell.movieTitleLabel.text = self.testArray![indexPath.item]
cell.movieYearLabel.text = "2016"
return cell
}
最佳答案
请尝试在每次更新数组时调用UICollectionView
的reloadData
。
如果您在 UITableView
中只使用一个单元格,那么您可以在 UITableView
的 cellForRowAtIndexPath
方法中重新加载,否则您可以在调用 之后调用
。UITableView
的 reloadData
关于UITableView 上的 iOS Swift ReloadData 不会刷新 UITableViewCell 内部的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713744/