嵌套在UITableViewCell中的UICollectionView在收到新数据

时间:2018-06-06 23:05:32

标签: ios swift uicollectionview

UICollectionView嵌套了UITableViewCell

enter image description here

集合视图单元格中的数字会在不同的视图中更新,因此当我返回到此屏幕时,我希望能够刷新视图,并且新数字会反映在其单元格中。我的集合视图中有一个名为topUserModel的模型,我使用firebase数据库中的数据填充。当我下拉刷新时,从我的主表视图中运行以下函数:

@objc func refreshView(refreshControl: UIRefreshControl) {

        DispatchQueue.main.async {
            //this is the row that the collection view is in
            if let index = IndexPath(row: 1, section: 0) as? IndexPath {
                if let cell = self.homeTableView.cellForRow(at: index) as? TopUserContainerViewController {
                    cell.userCollectionView.reloadData()
                }
            }
        }
        refreshControl.endRefreshing()
    }

然后在集合视图中运行我的awakeFromNib()触发:

func fetchTopUsers() {
    topUserModel.removeAll()
    let queryRef = Database.database().reference().child("users").queryOrdered(byChild: "ranking").queryLimited(toLast: 10)
    queryRef.observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String : AnyObject] {
            let topUser = TopUser(dictionary: dictionary)
            self.topUserModel.append(topUser)
        }

        DispatchQueue.main.async {
            self.userCollectionView.reloadData()
        }
    })
}

请注意,我要做的第一件事就是删除topUserModel中的所有数据。在存储新数据并附加它(见上文)之后,我可以在该代码块中打印出该整数的值以进行屏幕显示,并将其显示为更新值。 但是在我的集合视图中(见下文),如果我要在此处的任何点打印出整数值(它被称为watchTime),即使已擦除topUserModel,它仍会显示旧值干净的新数据已被添加?:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "topUserCell", for: indexPath) as! TopUsersViewController

        if topUserModel.count > indexPath.row {
            //image stuff redacted
            Nuke.loadImage(
                with: ImageRequest(url: url).processed(with: _ProgressiveBlurImageProcessor()),
                options: options,
                into: cell.topUserImage
            )
            cell.topUserName.text = topUserModel[indexPath.row].username
            cell.topUserMinutes.text = "\(String(describing: topUserModel[indexPath.row].watchTime!))"
        }
        return cell
    }

enter image description here

2 个答案:

答案 0 :(得分:1)

您不应该在dequeueReusableCell的任何地方拨打cellForRowAt

要获取当前显示的单元格(如果有),请使用cellForRowAt:;如果该行当前不在屏幕上,则可能会返回nil

获得单元格后,您可以重新加载数据并刷新其集合视图。

答案 1 :(得分:0)

您的密码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "topUserCell", for: indexPath) as! TopUsersViewController

        if topUserModel.count > indexPath.row {
            //image stuff redacted
            Nuke.loadImage(
                with: ImageRequest(url: url).processed(with: _ProgressiveBlurImageProcessor()),
                options: options,
                into: cell.topUserImage
            )
            cell.topUserName.text = topUserModel[indexPath.row].username
            cell.topUserMinutes.text = "\(String(describing: topUserModel[indexPath.row].watchTime!))"
        }
        return cell
    }

例如,如果当前indexPath为(1, 0),则会从indexPath (100, 0)重用该单元。因为它不在屏幕上,并且被重复用于显示新内容。

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "topUserCell", for: indexPath) as! TopUsersViewController
if topUserModel.count > indexPath.row {
   // ... Update to new content
}

// If topUserModel.count <= indexPath.row, then the cell content is undefined.
// Use the original content of reuse cell. 
// Ex, it may be come from (100, 0), but current indexPath is (1, 0).
// ...

// You can add a property to the cell to observe the behavior.
if (cell.indexPath != indexPath) {
    // Ops .... 
}

// Update to current indexPath
cell.indexPath = indexPath
return cell