Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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 UICollectionView未在swift中加载数组值_Ios_Uicollectionview_Swift4 - Fatal编程技术网

Ios UICollectionView未在swift中加载数组值

Ios UICollectionView未在swift中加载数组值,ios,uicollectionview,swift4,Ios,Uicollectionview,Swift4,UICollectionView数组值未加载到cellForItemAt indexPath中,我正在使用以下代码 var tableData: [String] = ["Evo X", "458", "GTR"] func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.tableData.count } f

UICollectionView数组值未加载到cellForItemAt indexPath中,我正在使用以下代码

var tableData: [String] = ["Evo X", "458", "GTR"]

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.tableData.count
}

func collectionView(_ collectionView: UICollectionView,  cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collections", for: indexPath) as! CollectionViewCell
   let indexPath = self.tableData[indexPath.row]
   print("indexpath\(indexPath)")
   cell.celllabel.text = self.tableData[indexPath.row]
   cell.backgroundColor = UIColor.cyan
   return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("You selected cell #\(indexPath.item)!")
       label.text = self.tableData[indexPath.row]
}
数组值打印在

打印(“indexpath(indexpath)”)

但没有载入

cell.celllable.text=self.tableData[indexPath.row]

我的输出是(控制台)

打印(“indexpath(indexpath)”)

但是这一行得到了错误

cell.celllable.text=self.tableData[indexPath.row]

我的错误是

 Fatal error: Unexpectedly found nil while unwrapping an Optional value
我如何解决这个问题

问题

let indexPath = self.tableData[indexPath.row]
cell.celllabel.text = self.tableData[indexPath.row]
解决方案

//let indexPath = self.tableData[indexPath.row] not needed
cell.celllabel.text = self.tableData[indexPath.row]
说明:

func collectionView(_ collectionView: UICollectionView,  cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collections", for: indexPath) as! CollectionViewCell
   cell.celllabel.text = self.tableData[indexPath.row]
   cell.backgroundColor = UIColor.cyan
   return cell
}
要在单元格的celllabel组件中显示的字符串数组。您可以从
indexPath.row
获取行,只需在索引
indexPath.row
处获取字符串即可

cell.celllabel.text = self.tableData[indexPath.row]
够了。希望能有帮助

编辑1:

func collectionView(_ collectionView: UICollectionView,  cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collections", for: indexPath) as! CollectionViewCell
   cell.celllabel.text = self.tableData[indexPath.row]
   cell.backgroundColor = UIColor.cyan
   return cell
}

CollectionViewCell是否有一个名为celllabel???的textField/textView?CollectionViewCell是否有一个Label感谢您的响应请尝试indexPath.item而不是indexPath.row。我已经尝试了这些行。但我也遇到了同样的问题。但是命令(//cell.celllabel.text=self.tableData[indexPath.row])将输出这些行。当我选择“didSelectItemAt indexPath”时,将显示值。但不在collectionviewcell中显示数组值row@ramkumar:我将发布CellForRowatineXpath的代码,只需将其替换为您的代码,如果需要,请告诉我helps@ramkumar:按原样使用我在编辑1中发布的cellForItemAt,让我知道它是否有效我是使用simulator@ramkumar:实现函数集合视图(uCollectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{并返回单元格大小以查看单元格和标签