【问题标题】:Same data is repeating in sections in UITableView and UICollectionView in swift 4swift 4 中的 UITableView 和 UICollectionView 部分重复相同的数据
【发布时间】:2023-03-14 09:25:01
【问题描述】:

我是快速开发应用程序的新手,我在 UITableView 内的 collectionView 中绑定数据, 我在 UICollectionView 中将数据(即图像)作为这样的多维数组发送

[[<UIImage: 0x600000292610>, {1280, 720}, <UIImage: 0x600000292bb0>, {1000, 563}, <UIImage: 0x600000292c50>, {2048, 1200}, <UIImage: 0x6000002926b0>, {1080, 675}, <UIImage: 0x600000292c00>, {1156, 577}, <UIImage: 0x60800028e650>, {322, 156}], [<UIImage: 0x60800028e290>, {1600, 840}, <UIImage: 0x60800028de30>, {1640, 878}, <UIImage: 0x600000294e10>, {322, 156}, <UIImage: 0x600000294d70>, {1917, 1080}, <UIImage: 0x600000295310>, {1311, 737}, <UIImage: 0x60800028dac0>, {258, 195}], [<UIImage: 0x600000295720>, {1601, 1281}, <UIImage: 0x600000295630>, {1600, 1341}, <UIImage: 0x60800028f500>, {1200, 919}, <UIImage: 0x600000296e90>, {4088, 2921}], [<UIImage: 0x608000290ae0>, {1600, 584}, <UIImage: 0x600000297160>, {1400, 1050}, <UIImage: 0x600000296b70>, {717, 476}, <UIImage: 0x608000290e50>, {2280, 1060}, <UIImage: 0x600000297390>, {1068, 427}, <UIImage: 0x608000291260>, {3200, 1600}, <UIImage: 0x608000290e00>, {258, 195}], [<UIImage: 0x608000291490>, {1300, 630}, <UIImage: 0x6080002914e0>, {512, 366}, <UIImage: 0x608000290bd0>, {1200, 628}], [<UIImage: 0x600000297b60>, {1200, 628}, <UIImage: 0x6000002972f0>, {1536, 1024}, <UIImage: 0x608000291b70>, {1540, 705}, <UIImage: 0x6000002985b0>, {1200, 676}, <UIImage: 0x60800009c4d0>, {443, 377}]]

我的问题是我看到第 0、1、2 节是唯一创建的,但是第 3 节的生成与第 0 节相同,

这是我在 collectionView 中的代码

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("numberOfItemsInSection ******",self.imageArray[section].count,"********")
         return self.imageArray[section].count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell: CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? CollectionViewCell
        {
           // if(imageArray[indexPath.section].count>0){
                print("---> ",indexPath.section," --- item ->",indexPath.row)
                cell.imageView.image = imageArray[indexPath.section][indexPath.row]
            //}
            return cell
        }
        return UICollectionViewCell()
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        print("numberOfSections ******",self.imageArray.count,"********")
        return self.imageArray.count
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        let size = CGSize(width: 120, height: 300)
        return size
    }

这是我的 tableView 控制器

extension CompanyViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if self.sectionHeaderName.count>0 {
            print("*******",self.sectionHeaderName[section],"*******")
            return self.sectionHeaderName[section]
        }else{
            return ""
        }
    }

    //NUMBER OF SECTION WE WANT IN A TABLE
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.sectionHeaderName.count
    }
    /*NUMNBER OF ROWS IN A SECTION*/
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? TableViewCell
        {

            return cell
        }
        return UITableViewCell()
    }
}

自 2 天以来我一直在这里苦苦挣扎,但找不到任何解决方案。 任何帮助将不胜感激

TIA

【问题讨论】:

  • 你不应该在UICollectionView中使用indexPath.row,而是使用indexPath.item。希望这能解决您的问题
  • 您好,我已更改为cell.imageView.image = imageArray[indexPath.section][indexPath.item],但它显示的结果相同。和return self.imageArray[section].count

标签: ios swift uicollectionview


【解决方案1】:

您应该在 TableViewCell willDisplay设置数据源或委托

func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
    collectionView.delegate = dataSourceDelegate
    collectionView.dataSource = dataSourceDelegate
    collectionView.reloadData()
}
override func tableView(tableView: UITableView,
    willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath) {

    guard let tableViewCell = cell as? TableViewCell else { return }

    tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}

【讨论】:

  • 嗨,添加此代码后开始抛出错误
  • 什么样的错误?顺便说一句,我的回答只是一个例子。您不应该直接复制和粘贴。
  • *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 我已经更改了一点并尝试了,我检查了标识符 cell 已经定义
  • 您应该像这样注册您的自定义集合视图单元collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
  • 我已经在情节提要中定义了单元格,是否需要以编程方式定义。因为当我定义使用这条线时,它在运行时再次抛出错误
【解决方案2】:

摸了3天终于找到答案了

我必须像这样在 cellForRowAt 函数中从 TableView 扩展中传递我的数据

cell.cellData = self.imageArray[indexPath.section]
return cell

在我定义 CollectionView 的 TableViewCell 中,我声明了变量

 var cellData = [UIImage]()

并使用相同的变量来获取所有集合视图函数中的图像

CellForItemAt 中需要像下面这样从数组中获取数据并使用行而不是项目和部分,因为当上下滚动 tableView 时,图像可能会乱序或错误地显示在单元格中,因此使用indexPath.row 可以防止图像的洗牌。

cell?.imageView.image = cellData[indexPath.row]

还有一点需要补充的是

cell.collectionView.reloadData() 

因为 TableView 引用了我的第一个数组索引,而不是我传递的内容。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多