Reputation: 2132
I have a problem: I have UITableView
with UITableViewCell
, at cell I have UICollectionView
with a different number UICollectionViewCell
. I want to UITableViewCell
height corresponds to the height of UICollectionView
.
I see real height UICollectionView
here:
class TimeViewCell: UITableViewCell {
@IBOutlet weak var labelTime: UILabel!
var dataForShow = [String]() {
didSet{
self.collectionView?.reloadData()
}
}
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
}
extension TimeViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataForShow.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CustomCollectionCell
cell.labelCollection.text = dataForShow[indexPath.row]
let realValueHeightOfCollection = collectionView.contentSize.height
//when print realValueHeightOfCollection I see 50.0 (if dataForShow.count <4) or 110.0 (if dataForShow.count >5)
return cell
}
}
but how to pass that height to height UITableViewCell
?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTime", for: indexPath) as! TimeViewCell
let showHour = self.infoWripper.sorted(by: { $0.hour < $1.hour })[indexPath.row]
cell.labelTime.text = showHour.showHour()
cell.dataForShow = showHour.showFullTime()
//here may be set the height of cell?
return cell
}
Upvotes: 0
Views: 1304
Reputation: 2132
I decided my problem by adding one line:
tableTime.rowHeight = cell.collectionView.collectionViewLayout.collectionViewContentSize.height
in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {...}
Upvotes: 2
Reputation: 8914
If you are using autolayout then you can try this...
self.tableView.estimatedRowHeight = <estimatedRowHeight>;
self.tableView.rowHeight = UITableViewAutomaticDimension;
UITableViewAutomaticDimension
will work if you've set leading, trailing, bottom, and top constraints relative to cell container view.
estimatedRowHeight
: set this value with possible row height.
When you have set these properties, you neither need to implement heightForRowAtIndexpath
nor estimatedRowHeight
.
Upvotes: 1
Reputation: 2419
I Have an approach for you: I used this in my code and m sharing with you
in TableView height method:
// MARK: - TableView Delegate Methods:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var height: CGFloat = 0.0
// // //print("arrTA[indexPath.row] = \(arrTA[indexPath.row])")
let dict = arrTA[indexPath.row] as! NSDictionary
let str = dict[ISSELECTED] as! String
let strFlage = dict[FLAGE] as! String
let arrFileCount = dict[DATA]!["file_list"] as! NSArray//dict.objectForKey(DATA)?.objectForKey("file_list").count as NSMutableArray
if(strFlage == "MA") {
height = 50.0
} else {
height = self.setExpandedViewHeight(arrFileCount.count) + 0.0
}
return height
}
And I used this function in managing height:
func setExpandedViewHeight(intArrCount: Int) -> CGFloat {
var height: CGFloat = 0.0
var generatedHeight: Int = 0
generatedHeight = intArrCount / 3 /// We get number of rows
if(intArrCount % 3 == 0) {
height = CGFloat(generatedHeight) * 100
} else {
height = (CGFloat(generatedHeight) + 1 ) * 100
}
return height
}
I hope you will read and debug the code at your end and execute it.
Upvotes: 2