CollectionCell: TableView Cell내부의 각 Cell을 구성하는 파일의 모음
TableCell: TableView Cell을 구성할 파일들의 모음
MainStoryboard에 가서 segue연결까지 완료해야함.
override func viewDidLoad() {
super.viewDidLoad()
setUpTableView()
}
override func awakeFromNib() {
super.awakeFromNib()
setUpCollectionView() //이름을 추후에 바꿈
}
실행화면
전체코드
import UIKit
class ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
var models = [Model]()
override func viewDidLoad() {
super.viewDidLoad()
setUpTableView()
}
//MARK: - nib파일 등록과 delegate, dataSource 선언
func setUpTableView() {
models.append(Model(imageName: "공기밥", title: "따끈따끈한 공기밥", price: "1000원"))
models.append(Model(imageName: "라면", title: "호로록 라면", price: "3500원"))
models.append(Model(imageName: "샤브샤브", title: "부글부글 샤브샤브", price: "11000원"))
models.append(Model(imageName: "공기밥", title: "따끈따끈한 공기밥", price: "1000원"))
models.append(Model(imageName: "라면", title: "호로록 라면", price: "3500원"))
models.append(Model(imageName: "샤브샤브", title: "부글부글 샤브샤브", price: "11000원"))
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "firstCollectionTableViewCell", bundle: nil), forCellReuseIdentifier: "firstCollectionTableViewCell") //nib 파일 등록
tableView.separatorStyle = .none //cell 구분선 없애기
}
}
//MARK: - 테이블 뷰 구성
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //cell의 갯수 설정
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //cell의 데이터 구성
let cell = tableView.dequeueReusableCell(withIdentifier: "firstCollectionTableViewCell", for: indexPath) as! firstCollectionTableViewCell
cell.ProductTitle.text = "이 상품 어때요?"
cell.configure(with: models)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { //cell의 높이 설정
return 240
}
}
import UIKit
class firstCollectionTableViewCell: UITableViewCell {
func configure(with models: [Model]){
self.models = models
collectionView.reloadData()
}
@IBOutlet weak var ProductTitle: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
var models = [Model]()
override func awakeFromNib() {
super.awakeFromNib()
setUpCollectionView()
}
//MARK: - nib파일 등록과 delegate, dataSource 선언
func setUpCollectionView() {
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: "firstCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "firstCollectionViewCell") //xib파일 등록
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension firstCollectionTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
models.count //model의 수 만큼 cell개수 설정
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCollectionViewCell", for: indexPath) as! firstCollectionViewCell
cell.configure(with: models[indexPath.row])
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize { //cell의 크기 설정
return CGSize(width: 150, height: 230)
}
}
import UIKit
class firstCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
//MARK: - 데이터 모델과 연결
public func configure(with model: Model){
self.ImageView.image = UIImage(named: model.imageName)
self.titleLabel.text = model.title
self.priceLabel.text = model.price
}
}
깃허브
- CollectionView-in-TableView(1줄)
- CollectionView-in-TableView(여러줄)
완성영상