Swift CollectionView内部表格视图
我在Swift CollectionView内部表格视图,swift,uitableview,uicollectionview,Swift,Uitableview,Uicollectionview,我在TableViewController单元格中有一个CollectionView,tableView包含3个部分,每个部分是不同的类别。如何在每个部分传递不同的数据。像Netflix应用程序 UICollectionViewController文件中的 导入UIKit 进口阿拉莫菲尔 类HomeViewController:UITableViewController、MovieDataDelegate{ 让tableViewHeaderIdentifier=“FeaturedTableHea
TableViewController
单元格中有一个CollectionView
,tableView
包含3个部分,每个部分是不同的类别。如何在每个部分传递不同的数据。像Netflix应用程序
UICollectionViewController文件中的
导入UIKit
进口阿拉莫菲尔
类HomeViewController:UITableViewController、MovieDataDelegate{
让tableViewHeaderIdentifier=“FeaturedTableHeader”
var homePresenter:homePresenter!
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
让headerNib=UINib(nibName:“FeaturedUITableViewHeaderView”,bundle:nil)
tableView.register(headerNib,forHeaderFooterViewReuseIdentifier:tableViewHeaderIdentifier)
homePresenter=homePresenter()
homePresenter.delegate=self
homePresenter.fetchData()
homePresenter.fetchImageData()
}
func arrayOfMoviesNames(名称:[字符串]){
//打印(homePresenter.homeData.moviesNamesArray)
tableView.reloadData()
}
func arrayOfPosters(路径:[字符串]){
//打印(homePresenter.homeData.posterPathsArray)
}
//MARK:-主UI表
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回1
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“featuredCell”,用于:indexath)作为!FeaturedTableViewCell
cell.homeCollectionView.dataSource=self
返回单元
}
重写func numberOfSections(在tableView:UITableView中)->Int{
返回3
}
重写func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView{
让headerView=tableView.dequeueReusableHeaderFooterView(标识符为tableViewHeaderIdentifier)作为!FeaturedUITableViewHeaderView
开关段{
案例0:
headerView.ishiden=true
案例1:
headerView.catagoryLabel.text=“热门电影”
案例2:
headerView.catagoryLabel.text=“名人”
违约:
headerView.catagoryLabel.text=”“作为字符串
}
返回头视图
}
重写func tableView(tableView:UITableView,heightForHeaderInSection:Int)->CGFloat{
如果节==0{
返回0
}
返回35
}
重写func tableView(tableView:UITableView,EstimatedHeightForFooterInstruction节:Int)->CGFloat{
返回12
}
重写func tableView(tableView:UITableView,willDisplay单元格:UITableViewCell,forRowAt indexath:indexPath){
guard let tableViewCell=单元格为?FeaturedTableViewCell else{return}
tableViewCell.setCollectionViewDataSourceDelegate(self,forRow:indexPath.row)
}
}
//标记:-collectionView
扩展HomeViewController:UICollectionViewDataSource、UICollectionViewDelegate{
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回homePresenter.homeData.moviesNamesArray.count
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:“homeCollectionCell”,for:indexath)作为!HomeCollectionViewCell
cell.posterImage.image=UIImage(名为:“阿拉丁”)
cell.movieName.text=“coco”
返回单元
}
}
UITableViewCell文件中的
导入UIKit
类FeaturedTableViewCell:UITableViewCell{
@IBVar homeCollectionView:UICollectionView!
重写func awakeFromNib(){
super.awakeFromNib()
//初始化代码
let collectionNib=UINib(nibName:“HomeCollectionViewCell”,bundle:nil)
homeCollectionView.register(collectionNib,forCellWithReuseIdentifier:“homeCollectionCell”)
}
覆盖功能设置选定(uSelected:Bool,动画:Bool){
super.setSelected(选定,动画:动画)
//为所选状态配置视图
}
}
扩展功能TableViewCell{
func setCollectionViewDataSourceDelegate(dataSourceDelegate:D,forRow row:Int){
homeCollectionView.delegate=dataSourceDelegate
homeCollectionView.dataSource=dataSourceDelegate
homeCollectionView.tag=行
homeCollectionView.reloadData()
}
}
我想为TabeView的每个部分显示不同的图像和文本使用
UITableView
数据源和UICollectionView
数据源方法
在UITableView
中设置3个部分,每个部分只有一个单元格
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Your cell with UICollectionView inside
let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_TABLE_VIEW_CELL_IDENTIFIER", for: indexPath)
// Set UICollectionViewDataSource, also add data source methods in your class, look at next code block
cell.collectionView.dataSource = self
return cell
}
// Header for each section,
func tableView(_ tableView: UITableView,
titleForHeaderInSection section: Int) -> String? {
if (section == 0) {
return nil
} else if (section == 1) {
return "Popular movies"
} else if (section == 2) {
return "Celebs"
}
}
如果要创建结构
电影
,并在视图控制器中创建元组数组。在元组中添加类别标题和相关电影数组。在表视图中,数据源方法使用主数组,在集合视图中,数据源方法使用对应元组中的movies数组。将当前元组/节索引设置为collectionView
标记。并在集合视图数据源方法中获取适当的movies数组
//HomeViewController
class HomeViewController: UITableViewController {
struct Movie {
var name: String
var image: UIImage?
//other details
}
var movieDetails:[(title:String, movies:[Movie])] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(FeaturedCell.self, forCellReuseIdentifier: "FeaturedCell")
movieDetails = [(title: "MoviesDB", movies: [Movie(name: "a", image: UIImage(named: "a")),
Movie(name: "b", image: UIImage(named: "b")),
Movie(name: "c", image: UIImage(named: "c"))]),
(title: "Popular Movies", movies: [Movie(name: "d", image: UIImage(named: "d")),
Movie(name: "e", image: UIImage(named: "e")),
Movie(name: "f", image: UIImage(named: "f"))]),
(title: "Celebs", movies: [Movie(name: "g", image: UIImage(named: "g")),
Movie(name: "h", image: UIImage(named: "h")),
Movie(name: "i", image: UIImage(named: "i"))])]
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return movieDetails.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return movieDetails[section].title
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FeaturedCell") as? FeaturedCell ?? FeaturedCell(style: .default, reuseIdentifier: "FeaturedCell")
cell.collectionView.tag = indexPath.section
cell.collectionView.delegate = self
cell.collectionView.dataSource = self
return cell
}
}
extension HomeViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return movieDetails[collectionView.tag].movies.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCell", for: indexPath) as? HomeCell ?? HomeCell()
let movie = movieDetails[collectionView.tag].movies[indexPath.item]
cell.posterImage.image = movie.image
cell.movieName.text = movie.name
return cell
}
}
//特征电池
class FeaturedCell: UITableViewCell {
var collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
}
collectionView.register(HomeCell.self, forCellWithReuseIdentifier: "HomeCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
}
//家庭细胞
class HomeCell: UICollectionViewCell {
let posterImage = UIImageView()
let movieName = UILabel()
//...
}
如果我想在CollectionView中的标签中显示文本,如第1节中的“Coco”和第2节中的“Alaaeldin”,您能给我一个代码示例吗谢谢您的回答。但我指的是收集视图数据的集合“可可”和“阿拉丁”。我用应用程序的照片更新了帖子
class HomeCell: UICollectionViewCell {
let posterImage = UIImageView()
let movieName = UILabel()
//...
}