最近,我正在嘗試做一些練習項目。因此,我觀看了斯坦福大學CS193P 2017版「開發iOS應用程序」課程。CollectionView在iOS中使用Swift 3的動態高度
現在,我正在做「Smashtag」項目,但是我遇到了一些問題。 我想通過兩個UICollectionViewFlowLayout使用CollectionView在SegmentedControl的每個xib中顯示兩個類型(一個像tableView,另一個顯示一個正方形的圖像)。
我的問題如下:
如何使CollectionViewCell顯示動態高度一樣的TableView的UITableViewAutomaticDimension?
而這就是我剛剛試過以下:
我試圖讓的CollectionView的自動調整設置像TableView中的autoDimension使用UICollectionViewFlowLayoutAutomaticSize。 (@available(iOS的10.0,*))
enum CollectionViewType {
case tweet
case image
}
// MARK: - Decide What Kind Of FlowLayout
func decideFlowLayout(type: CollectionViewType) -> UICollectionViewFlowLayout {
// just for .image type
var howManyImageShowing = 3
var imageShowingWidth: Double {
return Double(self.view.frame.width)/Double(howManyImageShowing)
}
// .tweet is a type showing like TableViewCell -> this make me confused !!
// another type is just showing a image in square -> I have no problem here
let estimatedItemSize = type == .tweet ? CGSize(width: self.view.frame.width, height: 155.0) :
CGSize(width: imageShowingWidth, height: imageShowingWidth)
let collectionViewLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
collectionViewLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
// --------- Make this setting as TableView does ---------
// ------------ Somethis just like this below ------------
/*
tableView.estimatedRowHeight = 155.0
tableView.rowHeight = UITableViewAutomaticDimension
*/
collectionViewLayout.estimatedItemSize = estimatedItemSize
collectionViewLayout.itemSize = UICollectionViewFlowLayoutAutomaticSize
// -------------------------------------------------------
collectionViewLayout.minimumLineSpacing = 0
collectionViewLayout.minimumInteritemSpacing = 0
return collectionViewLayout
}
我CollectionViewCell廈門國際銀行的約束在這裏: 但它有錯誤,當我運行這個程序
爲什麼這種解決方案將工作?
我已經尋找這個問題的很多解決方案堆棧溢出,但我只是覺得這,讓我真的很困惑:(
這個解決方案給視圖寬度約束這是廈門國際銀行CollectionViewCell和設置此約束的常數的價值和它的作品!我無法配置爲什麼寬度約束會使細胞的高度動態的?這讓我真的很困惑,我認爲這是奇怪...
class Cell: UICollectionViewCell {
@IBOutlet weak var headerLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.contentView.translatesAutoresizingMaskIntoConstraints = false
let screenWidth = UIScreen.main.bounds.size.width
widthConstraint.constant = screenWidth - (2 * 12)
}
}
如何做CollectionViewCell動態高度而不使用UICollectionViewFlowLayoutAutomaticSize。 (@available(iOS 10.0,*))?
因爲我想處理不同的iOS版本不僅在iOS 10中,而且在iOS 10以下。
我真的想在iOS開發和偉大的開發者親,但我還在學習和嘗試。任何答覆,我將不勝感激。
謝謝!
對不起,你的身高[indexPath.row]是什麼意思? – Kevin
它給你當前行的高度 –
但我沒有看到你的「高度」聲明在哪裏? – Kevin