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

时间:2015-06-06 00:22:26

标签: ios objective-c uitableview uicollectionview

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

2 个答案:

答案 0 :(得分:0)

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

e.g。

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有一个支持此类的类别)。

答案 1 :(得分: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()
         })
    }
}

这里我举一个例子来说明它的样子 enter image description here

相关问题