2017-08-03 83 views
0

我有一個UITableViewCell,其中我添加了UICollectionViewCell(用於水平滾動)。在TableViewCell中重新加載CollectionviewCell

如果沒有互聯網,我顯示空白視圖。我想要做的是,當用戶連接到互聯網時,我應該能夠顯示數據。 現在它隨機顯示,3箇中的2個單元根本不可見。 我正在使用通知用戶是否進入前臺並重新加載表數據。這裏是我的表格單元格和集合視圖單元格的結構。

代碼:

ViewController.swift:

class ViewController: UIViewController { 
    var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"] 
} 

extension ViewController : UITableViewDelegate { } 

extension ViewController : UITableViewDataSource { 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return categories[section] 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return categories.count 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow 
     return cell 
    } 

} 

CategoryRow.swift

class CategoryRow : UITableViewCell { 
    @IBOutlet weak var collectionView: UICollectionView! 
} 

extension CategoryRow : UICollectionViewDataSource { 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 12 
    } 

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

} 

extension CategoryRow : UICollectionViewDelegateFlowLayout { 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let itemsPerRow:CGFloat = 4 
     let hardCodedPadding:CGFloat = 5 
     let itemWidth = (collectionView.bounds.width/itemsPerRow) - hardCodedPadding 
     let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding) 
     return CGSize(width: itemWidth, height: itemHeight) 
    } 

} 

VideoCell.swift

class VideoCell : UICollectionViewCell { 

    @IBOutlet weak var imageView: UIImageView! 
} 
+0

確保你在故事板 –

+0

視圖 - 控制掛鉤委託和的CollectionView的數據源來的ViewController?我認爲它應該鉤到表格單元格上? –

+0

您需要將collectionview deleagte和數據源添加到tableview單元格中。然後在tableview單元的awaknib中重新加載collectionview。 – Hitesh

回答

0

UITableViewDataSource方法重新加載UICollectionView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow 
    cell.collectionView.reloadData() 
    return cell 
} 
+0

試過。沒有工作。 –