UITableView 上的 iOS Swift ReloadData 不会刷新 UITableViewCell 内部的 UICollectionView

标签 ios swift uitableview uicollectionview

我有一个 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
    }

最佳答案

请尝试在每次更新数组时调用UICollectionViewreloadData。 如果您在 UITableView 中只使用一个单元格,那么您可以在 UITableViewcellForRowAtIndexPath 方法中重新加载,否则您可以在调用 之后调用UITableView 的 reloadData

关于UITableView 上的 iOS Swift ReloadData 不会刷新 UITableViewCell 内部的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713744/

相关文章:

ios - 设置手势委托(delegate)时无法选择 map 对象

ios - 错误 : Value of type 'String' has no member 'URLByAppendingPathComponent'

ios - xcode 6.1 iOS 8.1 NSLocale displayNameForKey NSLocaleIdentifier 返回 nil

ios - 修复具有大量状态的巨型 View Controller

ios - iOS 上的 Crouton 等价物?

ios - Swift vs Objective-C 斐波那契数列速度比较

ios - 如何在ios中从手机中获取所有图像

ios - Admob WebFilter 日志,这是什么意思?

ios - TableView 自定义单元格未根据约束做出响应

iphone - 如何控制 UISearchDisplayController 的 searchResultsTableView?