在多个 collectionviewcells 中添加 tableview
Adding tableview inside multiple collectionviewcells
我 运行 我的应用程序有问题。我已经成功地制作了一个包含 5 个单元格的 CollectionView,水平滚动。在每个单元格中,我希望有一个 tableview 来显示从 Arduino 接收到的单独数据。但是我根本无法显示表格视图。
ALL 此视图中的代码是以编程方式编写的。
有一个帖子特别给了我希望自己找到答案。我在这里尝试了接受的答案:
但是,我还是没有工作。
由于我对 swift 缺乏经验,在代码设计方面更是如此,我怀疑存在一些限制问题,因为我以前经历过。
这是我的收藏视图代码:
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
// Returns how many items in one sections
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
// displays different colors in all the 5 sections
let colors: [UIColor] = [.blue, .green, .gray, .red, .orange]
cell.backgroundColor = colors[indexPath.item]
return cell
}
//Determines the size for each cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width , height: view.frame.height - 50 )
}
//Minimize the line spacing between the sections ... return 0 (no spacing)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
然后是tableview代码:
class ReceivedCell: UICollectionViewCell {
lazy var tableView: UITableView = {
let tv = UITableView()
tv.delegate = self
tv.dataSource = self
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
let tableViewCell = "tableViewCell"
var array = ["test", "test1", "test2"]
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blue
tableView.register(UITableViewCell.self, forCellReuseIdentifier: tableViewCell)
setupTableView()
//setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("Init(coder:) has not been implemented")
}
extension ReceivedCell: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell, for: indexPath)
cell.textLabel?.text = array[indexPath.item]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}
如有不明之处欢迎随时提问!我非常接近完成我的应用程序,我准备好所有其余代码以进入 tableview,我只需要能够显示它。非常感谢你们!
编辑
I want to have a tableview in every sections 1 - 5
我添加了一些代码,这样下面的vcs就可以如你所愿了。
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView.register(ReceivedCell.self, forCellWithReuseIdentifier: cellId)
// Do any additional setup after loading the view.
}
// Returns how many items in one sections
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ReceivedCell
print(cell.contentView.frame)
// displays different colors in all the 5 sections
let colors: [UIColor] = [.blue, .green, .gray, .red, .orange]
cell.backgroundColor = colors[indexPath.item]
return cell
}
//Determines the size for each cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width , height: view.frame.height - 50 )
}
//Minimize the line spacing between the sections ... return 0 (no spacing)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
class ReceivedCell: UICollectionViewCell {
lazy var tableView: UITableView = {
let tv = UITableView()
tv.delegate = self
tv.dataSource = self
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
let tableViewCell = "tableViewCell"
var array = ["test", "test1", "test2"]
override func didMoveToSuperview() {
super.didMoveToSuperview()
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: topAnchor, constant: 20),
tableView.bottomAnchor.constraint(equalTo: bottomAnchor,constant: -20),
tableView.leftAnchor.constraint(equalTo: leftAnchor,constant: 20),
tableView.rightAnchor.constraint(equalTo: rightAnchor,constant: -20),
])
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blue
addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: tableViewCell)
tableView.reloadData()
// setupTableView()
//setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("Init(coder:) has not been implemented")
}
}
extension ReceivedCell: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell, for: indexPath)
cell.textLabel?.text = array[indexPath.item]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}}
我 运行 我的应用程序有问题。我已经成功地制作了一个包含 5 个单元格的 CollectionView,水平滚动。在每个单元格中,我希望有一个 tableview 来显示从 Arduino 接收到的单独数据。但是我根本无法显示表格视图。
ALL 此视图中的代码是以编程方式编写的。
有一个帖子特别给了我希望自己找到答案。我在这里尝试了接受的答案:
由于我对 swift 缺乏经验,在代码设计方面更是如此,我怀疑存在一些限制问题,因为我以前经历过。
这是我的收藏视图代码:
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
// Returns how many items in one sections
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
// displays different colors in all the 5 sections
let colors: [UIColor] = [.blue, .green, .gray, .red, .orange]
cell.backgroundColor = colors[indexPath.item]
return cell
}
//Determines the size for each cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width , height: view.frame.height - 50 )
}
//Minimize the line spacing between the sections ... return 0 (no spacing)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
然后是tableview代码:
class ReceivedCell: UICollectionViewCell {
lazy var tableView: UITableView = {
let tv = UITableView()
tv.delegate = self
tv.dataSource = self
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
let tableViewCell = "tableViewCell"
var array = ["test", "test1", "test2"]
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blue
tableView.register(UITableViewCell.self, forCellReuseIdentifier: tableViewCell)
setupTableView()
//setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("Init(coder:) has not been implemented")
}
extension ReceivedCell: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell, for: indexPath)
cell.textLabel?.text = array[indexPath.item]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}
如有不明之处欢迎随时提问!我非常接近完成我的应用程序,我准备好所有其余代码以进入 tableview,我只需要能够显示它。非常感谢你们!
编辑
I want to have a tableview in every sections 1 - 5
我添加了一些代码,这样下面的vcs就可以如你所愿了。
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView.register(ReceivedCell.self, forCellWithReuseIdentifier: cellId)
// Do any additional setup after loading the view.
}
// Returns how many items in one sections
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ReceivedCell
print(cell.contentView.frame)
// displays different colors in all the 5 sections
let colors: [UIColor] = [.blue, .green, .gray, .red, .orange]
cell.backgroundColor = colors[indexPath.item]
return cell
}
//Determines the size for each cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width , height: view.frame.height - 50 )
}
//Minimize the line spacing between the sections ... return 0 (no spacing)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
class ReceivedCell: UICollectionViewCell {
lazy var tableView: UITableView = {
let tv = UITableView()
tv.delegate = self
tv.dataSource = self
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
let tableViewCell = "tableViewCell"
var array = ["test", "test1", "test2"]
override func didMoveToSuperview() {
super.didMoveToSuperview()
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: topAnchor, constant: 20),
tableView.bottomAnchor.constraint(equalTo: bottomAnchor,constant: -20),
tableView.leftAnchor.constraint(equalTo: leftAnchor,constant: 20),
tableView.rightAnchor.constraint(equalTo: rightAnchor,constant: -20),
])
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blue
addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: tableViewCell)
tableView.reloadData()
// setupTableView()
//setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("Init(coder:) has not been implemented")
}
}
extension ReceivedCell: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell, for: indexPath)
cell.textLabel?.text = array[indexPath.item]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}}