Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 相同的数据在swift 4的UITableView和UICollectionView的部分中重复_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 相同的数据在swift 4的UITableView和UICollectionView的部分中重复

Ios 相同的数据在swift 4的UITableView和UICollectionView的部分中重复,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我是swift的新手,正在开发一个应用程序,我正在UITableView中的collectionView中绑定数据, 我在UICollectionView中以多维数组的形式发送数据(即图像),如下所示 [[<UIImage: 0x600000292610>, {1280, 720}, <UIImage: 0x600000292bb0>, {1000, 563}, <UIImage: 0x600000292c50>, {2048, 1200}, <UIIm

我是swift的新手,正在开发一个应用程序,我正在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}]]
cell.cellData = self.imageArray[indexPath.section]
return cell
这是我的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()
    }
}
我在这里挣扎了两天,但找不到任何解决办法。 任何帮助都将不胜感激


TIA

您应该在TableViewCell中设置数据源或委派,单元格将显示

func setCollectionViewDataSourceDelegate(dataSourceDelegate:UICollectionViewDataSource&UICollectionViewDelegate,forRow行:Int){
collectionView.delegate=dataSourceDelegate
collectionView.dataSource=dataSourceDelegate
collectionView.reloadData()
}
覆盖函数表视图(表视图:UITableView,
willDisplayCell单元格:UITableViewCell,
forRowAtIndexPath indexPath:NSIndexPath){
guard let tableViewCell=单元格为?tableViewCell else{return}
tableViewCell.setCollectionViewDataSourceDelegate(self,forRow:indexPath.row)
}

我挠头三天后终于找到了答案

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

[[<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}]]
cell.cellData = self.imageArray[indexPath.section]
return cell
在定义CollectionView的TableViewCell中,我声明了变量

 var cellData = [UIImage]()
并使用相同的变量获取所有集合视图函数中的图像

CellForItemAt
中,需要从下面这样的数组中获取数据,并使用row而不是items和section,因为在上下滚动tableView时,图像可能会在单元格中出现混乱或错误显示,因此使用
Indexath.row
可以防止图像混乱

cell?.imageView.image = cellData[indexPath.row]
还有一件事我需要补充的是

cell.collectionView.reloadData() 
因为TableView引用的是我的第一个数组索引,而不是我传递的


谢谢

您不应该在
UICollectionView
中使用
indexPath.row
,而应该使用
indexPath.item
。希望这能解决您的问题我已经改成了这个
cell.imageView.image=imageArray[indexath.section][indexPath.item]
,但它显示了相同的结果。和
return self.imageArray[section].count
Hi添加此代码后开始抛出错误什么类型的错误?顺便说一下,我的回答只是一个例子。您不应直接复制和粘贴。
***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:“无法将种类的视图出列:UICollectionElementKindCell与标识符单元格-必须为标识符注册nib或类,或连接故事板中的原型单元格”
我做了一些更改并尝试,我检查了标识符
cell
已经定义。您应该像下面这样注册自定义集合视图单元格
collectionView.register(CollectionViewCell.self,forCellWithReuseIdentifier:“collectionCell”)
我已经在情节提要中定义了单元格,是否需要以编程方式定义。因为当我定义使用这一行时,它在运行时再次抛出错误