下载来自网址的数据时,在uitableviewcell内的重新加载集合视图

时间:2016-03-14 07:49:49

标签: ios swift uitableview uicollectionview

所以我在表视图单元格中有一个集合视图,没有在表格视图中下载来自url的类数据,让我们说图像被下载然后我需要在那些集合视图单元格上显示这些图像,所以我正在做的是重新加载表视图,但集合视图没有重新加载,我需要重新加载下载数据

1 个答案:

答案 0 :(得分:0)

确实这很简单(假设你有viewController,customTableViewCell和customCollectionViewCell正常工作),在控制器中使用此代码,只要你需要在表格视图单元格中重新加载collectionViews就可以声明表格视图:

//在ViewController中

var myRow = 0 // set the index path row of your tableview cell
var mySection = 0 // and the index path section 

 DispatchQueue.main.async(execute: {
        if let index = IndexPath(row: myRow, section: mySection) as? IndexPath {
             if let cell = self.MyTableView.dequeueReusableCell(withIdentifier: "cell", for: index) as? MyCustomTableViewCell {
                cell.collectionReloadData()
             }
       }
 })

现在在你的TableViewCell

class MyCustomTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionView: UICollectionView!
    func collectionReloadData(){
         DispatchQueue.main.async(execute: {
            self.collectionView.reloadData()
         })
    }

//Dont forget to set delegates    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
    }    
}

这里有一个代码实例,第3节" Cartas Recomendadas"中的集合视图。被重新加载:

enter image description here