【发布时间】:2018-03-28 17:53:53
【问题描述】:
我正在尝试将一些水平滚动的图像放入 tableview 单元格中,所以我使用了一个 collectionview,我将它放入了单元格中,除了单元格高度之外,一切正常。我已经在故事板中选择了要自定义的单元格高度,并且也使用代码更改了单元格高度,但它仍然不起作用。我是菜鸟,找不到问题出在哪里,请大家帮帮我,谢谢! 1.I want to change the cell height 2.Stroyboard
class HomePageTableViewController: UITableViewController {
var ImageArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
ImageArray = ["Calendar.png","Calendar.png","Calendar.png","Calendar.png","Calendar.png"]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ImageArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BannerCell") as! BannerTableViewCell
return cell
}
override func viewWillAppear(_ animated: Bool) {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
}
extension HomePageTableViewController: UICollectionViewDelegate,
UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ImageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
-> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BannerCollection", for: indexPath) as! BannerCollectionCollectionViewCell
cell.BannerImage.image = UIImage(named: ImageArray[indexPath.row])
return cell
}
}
【问题讨论】:
-
TableViews 有一个名为
heightForRowAt: IndexPath的函数调用该函数并在其中设置高度。如果集合中有多个单元格,CollectionViews 有一个称为flowLayout的东西。 -
你的代码中没有单元格,它是一个单独的tableView和collectionView
-
您的收藏视图在哪里?您是否将其添加到 BannerCell 自定义表格视图单元格中?您在哪里为您的 collectionview 设置了数据源和委托?
-
@Shawn 我创建了两个名为 BannerTableViewCell 和 BannerCollectionViewCell 的类,并将表格视图单元格标识符设置为“BannerCell”,并且在情节提要中集合可重用视图标识符为“BannerCollection”,数据源和委托已连接到表格视图控制器。
标签: ios iphone swift xcode swift4