表格视图单元格中的集合视图重新加载数据。

3

我有一个UICollectionView位于UITableViewCell内,其中集合视图的图像从后台线程中获取。 我遇到的问题是,无法调用collectionView.reloadData,因为我无法设置UICollectionView的属性,因为它位于tableViewCell中。 有什么办法在后台线程完成查询后重新加载集合视图的数据吗?

2个回答

0

子类化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具有支持此功能的类别)。

0

确实很容易(假设您的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()
         })
    }
}

这里我给你一个例子,看起来是这样的 在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接