繁体   English   中英

tableview单元格内的集合视图重新加载数据

[英]collection view inside tableview cell reload data

我在UITableViewCell中有一个UICollectionView,并且正在从后台线程上的parse中检索集合视图的图像。 我遇到的麻烦是我无法调用集合视图reloadData,因为我无法设置UICollectionView的属性,因为它在一个表视图单元格内。任何想法如何重新加载集合视图的数据之后后台线程已完成执行查询?

子类UITableViewCell并添加方法和对其集合视图的引用。 处理你的电话。 我假设您在表视图控制器上创建了集合视图属性

例如

CollectionViewTableCell.h:

@interface CollectionViewTableCell : UITableViewCell
@end

CollectionViewTableCell.m:

@interface CollectionViewTableCell ()
    @property (nonatomic, weak) @IBOutlet UICollectionView* collectionView;
@end

@implementation CollectionViewTableCell
-(void) awakeFromNib {
    [self loadData];
}
-(void) loadData {
    // load parse data asynchronously and then call reloadData on your collection view
}

 // make sure to implement delegate/datasource methods for your collection view
@end

请注意,有一些方法可以将图像URL发送到UIImageView,图像将从URL异步加载(例如AFNetworking有一个支持此类别的类别)。

确实很简单(假设你的viewController,tabbleViewCell和CollectionViewCell正常工作),在你声明表视图的控制器中使用这段代码:

对于Swift 4

//在ViewController中随时需要重新加载collectionViewCell:

var myRow = 0 // include the index path row of your table view 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? TableViewCell {
                cell.collectionReloadData(section: mySection)
             }
       }
})

现在在你的TableViewCell中

class TableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

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

在这里,我举一个例子来说明它的样子 在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM