【发布时间】:2021-01-30 14:35:57
【问题描述】:
我正在尝试将两个具有自己偏好的现有集合视图单元格添加到我已经存在的表格视图的前两个单元格中,而不会丢失其他表格视图单元格。现在每个视图都可以手动运行。 This is my UI
系列 V1:
import UIKit
final class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var images: UIImageView!
public func configureCollection(photo: UIImage) {
images.image = photo
images.layer.cornerRadius = 12
}
}
系列 V2:
import UIKit
final class SelectCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var selectionTextLabel: UILabel!
public func confCollectionView(selectionText: String){
selectionTextLabel.text = selectionText
}
}
表格视图
import UIKit
final class homePageTableView: UITableViewCell {
@IBOutlet weak var productPhoto: UIImageView!
@IBOutlet weak var productName: UILabel!
public func configureHomeShop(photo: UIImage, item: String) {
productName.text = item
productPhoto.image = photo
}
}
查看所有内容所在的控制器:
class homeView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var collectionViewOne: UICollectionView!
@IBOutlet weak var collectionViewTwo: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionViewOne.register(CollectionViewCell.self, forCellWithReuseIdentifier: "itemsCollectionViewCell")
collectionViewTwo.register(SelectCollectionViewCell.self, forCellWithReuseIdentifier: "SelectCollectionViewCell")
collectionViewTwo.dataSource = self
collectionViewTwo.delegate = self
collectionViewOne.dataSource = self
collectionViewOne.delegate = self
tableView.dataSource = self
tableView.delegate = self
}
// **Functions**
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "shopCell", for: indexPath) as? homePageTableView else {
fatalError()
}
cell.configureHomeShop(photo: photoArrayTV.first!, item: itemsArray.first!)
return cell
}
// MARK: - CollectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionViewOne{
return imagesArray.count
}else{
return selectionArray.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewOne{
guard let cellOne = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCollectionViewCell", for: indexPath) as? CollectionViewCell else{
fatalError()
}
cellOne.configureCollection(photo: imagesArray[imagesArray.count - indexPath.row - 1])
return cellOne
}else{
guard let cellTwo = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCollectionViewCell", for: indexPath) as? SelectCollectionViewCell else{
fatalError()
}
cellTwo.confCollectionView(selectionText: selectionArray.last!)
return cellTwo
}
}
}
【问题讨论】:
标签: ios swift uicollectionview tableview uicollectionviewcell