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
181 views
in Technique[技术] by (71.8m points)

ios - TableView with embedded CollectionView

How to reload whole single row?

first time when table view reload at that time table view call number of row in section time now collection view call number of times but vertically horizontally it's calling once

how to call the whole row in a single time

collection view loading this vertically with numbers of time

it should be load like following ways

collection view loading horizontally single element only

    extension HomeScreen: UITableViewDataSource, UITableViewDelegate, UITableViewDataSourcePrefetching {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        10
    }

    func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
        print(indexPaths)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "StoryCell") as? StoryTableViewCell
        cell?.remixDelegate = self
        cell?.reloadAllData(index: indexPath.row)
        return cell!
    }

     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print("TableView Will display",indexPath.row)
        guard cell is StoryTableViewCell else { return }

    }

    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        guard let tableViewCell = cell as? StoryTableViewCell else { return }
        tableViewCell.pauseAllCell()

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return storyTableView.frame.height
    }
}


custom class of table view in which collection view methods written

protocol RemixDelegate: NSObjectProtocol {
    func onRemix(errorMessage: String)
}
class StoryTableViewCell: UITableViewCell {

    @IBOutlet weak var remixCollectionView: UICollectionView!
    weak public var remixDelegate: RemixDelegate?
    @objc dynamic var currentIndex = 0
    @objc dynamic var oldIndex = 0
    var oldAndNewIndices = (0, 0)
    var urlArray = [
                    "http://134.209.145.75:4003/storage/posts/d81ef5f0-957b-5d91-b7cf-ffe5e02d687b.mp4",
                    "http://134.209.145.75:4003/storage/posts/7649cfab-0e58-51ca-baea-633ab09f653e.mp4",
                    "http://134.209.145.75:4003/storage/posts/98afae13-7858-5006-b8e4-c3f3e822d33e.mp4",
                    "http://134.209.145.75:4003/storage/posts/352edd21-1f7e-5fb1-b87b-5fe78bbece53.mp4",
                    "http://134.209.145.75:4003/storage/posts/183d3022-f938-58d0-b2fa-aff01f20f13e.mp4"]

    override func awakeFromNib() {
        super.awakeFromNib()
        let screenSize = UIScreen.main.bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height
        self.remixCollectionView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        let nibCellSpeciality = UINib(nibName: "RemixCollectionViewCell", bundle: nil)
        remixCollectionView.register(nibCellSpeciality, forCellWithReuseIdentifier: "RemixCell")
        self.remixCollectionView.performBatchUpdates({
            remixCollectionView.reloadItems(at: [IndexPath(item: 0, section: 0),IndexPath(item: 1, section: 0),
                                                 IndexPath(item: 2, section: 0),
                                                 IndexPath(item: 3, section: 0),
                                                 IndexPath(item: 4, section: 0)])
                        }, completion: nil)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    func reloadDataOfTopOfView(index:Int){

    }
    func pauseAllCell() {
        self.remixCollectionView?.visibleCells.forEach { cell in
            if let cell = cell as? RemixCollectionViewCell {
                cell.pause()
            }
        }
    }

    func reloadAllData(index: Int) {
        self.remixCollectionView?.scrollToItem(at: NSIndexPath(item: 0, section: 0) as IndexPath,
                                               at: .left,
                                               animated: true)

        self.remixCollectionView?.reloadData()
    }

    @objc func onRemix(sender: UIButton) {
        let buttonRow = sender.tag
        print(buttonRow)
        remixDelegate?.onRemix(errorMessage: "")
    }
}

here is my custom class of table view

// MARK: - UICollection View Delegates
extension StoryTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDataSourcePrefetching, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return urlArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print("CellForRowAt",indexPath.row)
        let cell: RemixCollectionViewCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "RemixCell", for: indexPath) as? RemixCollectionViewCell)!
        cell.configure(url: urlArray[indexPath.row])
        cell.remixButton.tag = indexPath.row
        cell.remixButton.addTarget(self,action:#selector(onRemix(sender:)), for: .touchUpInside)
        return cell
    }

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Collection view at row (collectionView.tag) selected index path (indexPath)")
    }

         func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        print("willDisplay", indexPath.row)
        if let cell = cell as? RemixCollectionViewCell {
            oldAndNewIndices.1 = indexPath.row
            currentIndex = indexPath.row
            cell.pause()
        }
    }

         func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        print("didEndDisplaying", indexPath.row)
        if let cell = cell as? RemixCollectionViewCell {
            cell.pause()
        }
    }

        func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
        print("prefetchItemsAt", indexPaths)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let size = CGSize(width: collectionView.layer.frame.width, height: collectionView.layer.frame.height)
        return size
    }
}
// MARK: - ScrollView Delegates
extension StoryTableViewCell: UIScrollViewDelegate {
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
            let cell =  self.remixCollectionView.cellForItem(at: 
     IndexPath(row: self.currentIndex, section: 0)) as? 
  RemixCollectionViewCell
        cell?.replay()
    }

}

I just want to reload single row of collectionview

question from:https://stackoverflow.com/questions/65884538/tableview-with-embedded-collectionview

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

1.4m articles

1.4m replys

5 comments

56.9k users

...