3

I have a UITableView contains one horizontal scroll UICollectionView in every cell like this

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ServiceTypeCell
    cell.collectionView.delegate = self
    cell.collectionView.dataSource = self
    cell.lblName.text = serviceTypes[indexPath.row].name
    cell.tag = getTag(indexPath)
    print("uitableviewcell : \(indexPath)")
    return cell
}

And in every UICollectionViews

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let cell = collectionView.superview?.superview as! ServiceTypeCell
    let indexPath = getIndexPath(cell.tag)
    print("uitableviewcell from uicollectionview : \(indexPath)")
    return serviceTypes[indexPath.row].services.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let tableViewCell = collectionView.superview?.superview as! ServiceTypeCell
    let tableViewIndexPath = getIndexPath(tableViewCell.tag)
    let service = serviceTypes[tableViewIndexPath.row].services[indexPath.row]
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ServiceTypeDetailCell
    cell.lblName.text = service.name
    return cell
}

My problem is UICollectionView Datasource only called between UITableViewCell index 0 to 3. Why does this happen?

My print debug result like this

uitableviewcell : [0, 0]
uitableviewcell from uicollectionview : [0, 0]
uitableviewcell : [0, 1]
uitableviewcell from uicollectionview : [0, 1]
uitableviewcell : [0, 2]
uitableviewcell from uicollectionview : [0, 2]
uitableviewcell : [0, 3]
uitableviewcell from uicollectionview : [0, 3]
uitableviewcell : [0, 4]
uitableviewcell : [0, 5]
uitableviewcell : [0, 6]
Vinod Kumar
  • 3,375
  • 1
  • 17
  • 35
Mat Yus
  • 289
  • 1
  • 2
  • 14
  • Did you set the scroll direction of UICollectionView as horizontal? – Imad Ali May 16 '17 at 07:54
  • I think first stop the tableviewDidselect. Then select the collection view cell. – phani May 16 '17 at 07:54
  • @Imad yes I already set horizontal scroll in storyboard – Mat Yus May 16 '17 at 07:56
  • @PhaniRaghu What do you mean stop tableviewDidSelect? This tableview does not need any selection – Mat Yus May 16 '17 at 07:57
  • Sry I misunderstand the question. – phani May 16 '17 at 07:59
  • What is your `getTag` method doing. @MatYus – iPeter May 16 '17 at 08:23
  • @iPeter it is my method to use indexpath as tag,combine section and row as one integer – Mat Yus May 16 '17 at 08:36
  • Because `collectionViews` are being reused. How many cells are visible at once the same number of `collectionViews` are being created so delegates will be call for those only. What happens if write `cell.collectionView.reloadData()` in `cellForRowAtIndexPath:` just after `cell.collectionView.dataSource = self` line.? – TheTiger Aug 11 '17 at 05:37

3 Answers3

2

Not sure , whether this can Resolve your issue or not , but having consuming so much time finally i found the answer why CollectionView delgate method not gets called when it is embed inside TableView :

we basically use these two :

  • UICollectionViewDataSource

  • UICollectionViewDelegate

but forget to conform this : UICollectionViewDelegateFlowLayout

After conforming this (UICollectionViewDelegateFlowLayout) did solve my issue .

Hope it will solve other issue too. Feel free to comment and share your feedback. Thanks.

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
1

This happens because your collection view instances are reused with cells. Only 4 instances are being created and reused.

You can write your collectionview logic in your custom tableview cell and make custom cell as delegate and datasource of collectionview.

Mr. SS
  • 387
  • 2
  • 19
  • Yes I'm aware of that, its only happen in iPhone5. Do you have any idea how to fix this? – Mat Yus May 16 '17 at 07:59
  • 1
    write collection view datasource and delegate methods in ServiceTypeCell cell and assign delegate and datasource as cell to collectionview. Also make a 'service' property in cell and assign data to cell in cellForRowAtIndexpath – Mr. SS May 16 '17 at 08:01
  • I try move uicollectionview datasource into ServiceTypeCell class, still got same result. in my uitableviewcell cellForRowAtIndexpath: cell.serviceType = serviceTypes[indexPath.row] cell.collectionView.delegate = cell cell.collectionView.dataSource = cell – Mat Yus May 16 '17 at 08:29
  • the only solution i got is set cell.collectionView.reloadData() in cellForRowAtIndexpath but it will reset the position of cell to 0 again if i scroll top and back to this cell – Mat Yus May 16 '17 at 08:41
0

Write collection view code on cell class. And give collectionview delegate to cell because cell is super class of collection view. Check this screen shot enter image description here

Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36