I have a UITableView and Inside that I have two UICollectionViews
.
My requirement is that I have to scroll first UICollectionView
Horizontally.
And I have to scroll UITableView
vertically Because I don't want to scroll Second UICollectionView
.
In Image you can see that first (Header 'Folder') UICollectionView
I have to scroll horizontally and second (Header 'Product') UICollectionView
I don't want to scroll vertically because I have to scroll Complete UITableView
.
Both collectionView
are in TableViewCell
and I have disable scrolling of second UICollectionView
.
I have to add and delete a product then I how can I set the TableViewSecond
Cell height that content should come.
Here is my code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (0 == indexPath.section) {
var addFolderCell = tableView.dequeueReusableCell(withIdentifier: "AddFolderCell") as? AddFolderCell
if(nil == addFolderCell) {
let nib:Array = Bundle.main.loadNibNamed("AddFolderCell", owner: self, options: nil)!
addFolderCell = nib[0] as? AddFolderCell
}
addFolderCell?.collectionView.delegate = self
addFolderCell?.collectionView.dataSource = self
return addFolderCell!
} else {
let identifier = "CreateFolderCell"
var createFolderCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? CreateFolderCell
if(createFolderCell == nil) {
let nib:Array = Bundle.main.loadNibNamed("CreateFolderCell", owner: self, options: nil)!
createFolderCell = nib[0] as? CreateFolderCell
}
return createFolderCell!
}
}
//Height for row.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//let index : NSIndexPath = NSIndexPath(row: 0, section: 1)
if 0 == indexPath.section {
return 135
}
return 300
}
//Header height.
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 20
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionsArray[section] as? String
}
}
extension GroupDataView : UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddFolderCollectionCell", for: indexPath) as! AddFolderCollectionCell
//cell.backgroundColor = UIColor.white
cell.layer.borderWidth = 0.4
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.cornerRadius = 2
//CreateCardView(viewCorner: cell.cardView) //cell.cardView
return cell
}
}
//MARK: Collection View Layout
extension GroupDataView : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 90, height: 130)
}
}
What can I do? How can I set the height for tableView?
I think you can accomplish this, using some few steps:
Subclass your
UITableViewCell
, let's name itCollectionCell
, and change it's class in IB interface to new subclass name.Create outlet from
UICollectionView
toCollectionCell
subclass, whereUICollectionView
is placed. Let's name itcollectionView
.Add constraints to all sides of
collectionView
to cell bounds. (top, right, bottom, left).Add in IB for
collectionView
height constraint and create outlet toCollectionCell
too. Let's name itcollectionViewHeight
.In
tableView:cellForRowAtIndexPath:
add code: