Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

ios - UICollectionView inside TableViewCell not called

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...