首页 > 解决方案 > 带有嵌入式 CollectionView 的 TableView

问题描述

如何重新加载整个单行?

表视图第一次在那个时间重新加载表视图调用部分时间的行数现在集合视图调用次数但垂直水平它调用一次

如何一次调用整行

集合视图垂直加载此时间数

它应该像以下方式加载

集合视图仅水平加载单个元素

    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: "")
    }
}

这是我的自定义表格视图类

// 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()
    }

}

我只想重新加载单行collectionview

标签: iosswiftuicollectionviewtableview

解决方案


推荐阅读