在 TableViewCell 中重新加载 CollectionviewCell [英] Reload CollectionviewCell inside TableViewCell
问题描述
我有一个 UITableViewCell,其中添加了 UICollectionViewCell(用于水平滚动).
I have a UITableViewCell inside which I have added UICollectionViewCell ( for horizontal scrolling).
如果没有互联网,我将显示空白视图.我想做的是当用户连接到互联网时,我应该能够显示数据.现在它随机显示,3 个中的 2 个根本不可见.我正在使用通知用户是否进入前台并重新加载表数据.这是我的表格单元格和集合视图单元格的结构.
If there is no internet I am showing a blank view. What I wanna do is when a user connects to the internet I should be able to show data. Right now it shows randomly and 2 cells out of 3 are not visible at all. I am using notification whether the user came in foreground and reloading table data. Here is my structure of table cell and collection view cell.
代码:
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!
}
推荐答案
在 UITableViewDataSource
方法中重新加载 UICollectionView
.
In UITableViewDataSource
method reload UICollectionView
.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardTableViewCell", for: indexPath) as! DashboardTableViewCell
cell.collectionViewNeel.reloadData()
return cell
}
这篇关于在 TableViewCell 中重新加载 CollectionviewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!