ios - 如何在 uiTableViewCell 中添加 collectionView
问题描述
我正在尝试在 uitableView 单元格内添加一个带有 5 个单元格的 uicollectionView。我遇到了集合视图根本不显示的问题。这是我的代码:
// 表视图控制器
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath) as! BookTableViewCell
cell.cellIndex = indexPath
cell.dataSource = self
cell.delegate = self
cell.backgroundColor = UIColor.white
// if let books = all_books[indexPath.section], books.count > 0 {
// cell.collectionView.reloadData()
// }
return cell
}
// BookTableViewCell
import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage
class BookTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
var delegate: BookTableViewCellDelegate?
var dataSource: BookTableViewCellDelegate?
let leftAndRightPaddings: CGFloat = 10.0
let numberOfItemsPerRow: CGFloat = 5
let screenSize: CGRect = UIScreen.main.bounds
var collectionView: UICollectionView!
var tableRowData: Book!
var books : [Book]?
var ref: DatabaseReference!
var cache:NSCache<AnyObject, AnyObject>!
var cellIndex: IndexPath!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.itemSize = CGSize(width: 100, height: self.frame.height)
//You can also provide estimated Height and Width
flowLayout.estimatedItemSize = CGSize(width: 100, height: self.frame.height)
//For Setting the Spacing between cells
flowLayout.minimumInteritemSpacing = 25
flowLayout.minimumLineSpacing = 20
collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.showsHorizontalScrollIndicator = false
collectionView.backgroundColor = UIColor.clear
self.addSubview(collectionView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath) as! BookCollectionViewCell
self.dataSource?.getData(cell: self)
let books = self.books
let book = books?[indexPath.row]
if book != nil {
cell.imageView.sd_setImage(with: URL(string: book?.image as! String), placeholderImage: nil)
cell.label.text = book?.title
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: 100, height: self.frame.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.dataSource?.getData(cell: self)
let books = self.books
let book = books?[indexPath.row]
if let book = book {
self.delegate?.didSelectCollection(book: book)
}
}
}
protocol BookTableViewCellDelegate {
func getData(cell: BookTableViewCell)
func didSelectCollection(book: Book)
}
有人可以帮忙吗?我在 init 函数中初始化集合视图。正确设置所有代表。不知道我做错了什么。
解决方案
问题是
collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout)
你需要在正确的时间设置框架而不是在init
里面
override func layoutSubviews() {
super.layoutSubviews()
collectionView.frame = self.bounds
}
或更好地在内部使用约束init
,也将其添加到
self.contentView.addSubview(collectionview)
推荐阅读
- javascript - 损坏的 JPG,超出缓冲区限制
- c# - 部分继承包含的 xml 注释
- arm - STM32F4系列ARM处理器
- javascript - 我在这段代码中遇到了错误。这个想法是计算该单词在给定段落中重复的次数
- liferay-7 - 如何在 Liferay 7.3 中为页面片段添加自定义布局
- python - 如何使用 Django 从 QuerySet 中提取特定记录?
- laravel - 在 laravel 中为推送通知创建 reactphp 套接字
- javascript - Node.js 请求等效于 jQuery AJAX .complete()
- c++ - 具有交替位的二进制数,错误答案
- ios - 在 UIKit 中实现水平滚动视图?