【发布时间】:2019-11-17 04:25:56
【问题描述】:
1.我有一个包含两个单元格的表格视图。我想要这两个单元格的高度自动尺寸。在第一个单元格中,有一个标签,根据标签,单元格的高度会增加。在第二个单元格中,有一个集合视图,其中包含用户照片。随着照片数量的增加,collection view 的高度会随之增加,table view cell 的大小也会随之增加。
2。我有以下代码,但没有得到想要的结果。在我的代码中,我有两个表格视图单元格“CustomCell”和“图像单元格”。图像单元格有一个集合视图。在表格视图单元格中获取集合视图是一种好习惯,还是应该在这里使用滚动视图?但是随着照片数量的增加,collection view 的高度也会增加。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell
cell?.personName.text = "New User"
cell?.aboutPerson.text = "Near the beginning of his career"
return cell!
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as? imageCell
return cell!
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var height:CGFloat = CGFloat()
if indexPath.row == 0 {
return UITableView.automaticDimension
}
else {
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath) as? imageCell
return (cell?.personPhotoCollectionView.frame.size.height)!
}
return height
}
}
class imageCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 50
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = personPhotoCollectionView.dequeueReusableCell(withReuseIdentifier: "imageCollectionViewCell", for: indexPath) as? imageCollectionViewCell
cell?.personImage.image = UIImage(named: "azamat-zhanisov-om_aSsajsLw-unsplash.jpg")
return cell!
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let numberOfCollumns: CGFloat = 3
let width = collectionView.frame.size.width
let xInsets: CGFloat = 10
let cellaSpacing: CGFloat = 5
return CGSize(width: (width / numberOfCollumns) - (xInsets + cellaSpacing), height: (width / numberOfCollumns) - (xInsets + cellaSpacing))
}
@IBOutlet weak var personPhotoCollectionView: UICollectionView!
}
【问题讨论】:
标签: ios swift uitableview uicollectionview