Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 CollectionView第一个单元格添加用户按钮和图像阵列中的其他单元格加载_Ios_Swift - Fatal编程技术网

Ios Swift CollectionView第一个单元格添加用户按钮和图像阵列中的其他单元格加载

Ios Swift CollectionView第一个单元格添加用户按钮和图像阵列中的其他单元格加载,ios,swift,Ios,Swift,在我的例子中,我试图创建一个uicollectionview,第一个单元格添加按钮用于添加用户,第二个单元格需要将图像数组字符串加载到另一个单元格中。在这里,我使用两个独立的单元格,但我并没有将第一个单元格放入我的输出中。请给我提供解决方案或其他最好的方法 我正在努力降低产量 多单元代码 // MARK: CollectionView Delegate func numberOfSectionsInCollectionView(collectionView: UICollectionView)

在我的例子中,我试图创建一个
uicollectionview
,第一个单元格添加按钮用于添加用户,第二个单元格需要将图像数组字符串加载到另一个单元格中。在这里,我使用两个独立的
单元格
,但我并没有将第一个单元格放入我的输出中。请给我提供解决方案或其他最好的方法

我正在努力降低产量

多单元代码

// MARK: CollectionView Delegate
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 2
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.section == 0 {
        let firstCell = collectionView.dequeueReusableCell(withReuseIdentifier: "addusercell", for: indexPath) as! AddParticipantsCollectionViewCell
        return firstCell
    } else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "userscollectionview", for: indexPath as IndexPath) as! ParticipantsCollectionViewCell
        cell.imageView?.image = self.imageArray[indexPath.row]
        return cell
    }
}
你需要

if indexPath.item == 0  {
    // first cell
}
else {

   // second cell
   cell.imageView?.image = self.imageArray[indexPath.item - 1]
}

正确的数据源方法(删除它

并将节中的项目数更改为

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageArray.count + 1
}

如果collectionView中有两个部分,则还需要设置每个部分中的项目数:

    // MARK: CollectionView Delegate
    func numberOfSections(in collectionView: UICollectionView) -> Int {
      return 2
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      if section == 0 {
        return 1 // return 1 item in cell (+ cell)
      } else {
        return imageArray.count
      }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0 {
            let firstCell = collectionView.dequeueReusableCell(withReuseIdentifier: "addusercell", for: indexPath) as! AddParticipantsCollectionViewCell
            return firstCell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "userscollectionview", for: indexPath as IndexPath) as! ParticipantsCollectionViewCell
            cell.imageView?.image = self.imageArray[indexPath.row]
            return cell
        }
    }

我希望它能有所帮助。

如果我从数组中添加
indexPath.item==0
可能有机会跳过第一个索引,对吗?这是一个好方法吗?我认为应该从数组中加载第一个始终是静态索引和其他项,因为'item 0'始终需要可见,但其他
仅基于
JSON
数据可用。当访问数组时,从
indexPath.item
中减去1,将在
indexPath.section==0&&indexPath.item==0
它显示每个单元格3个图像,因此总共显示6个。我需要在哪里减法?你能更新你的密码吗。
    // MARK: CollectionView Delegate
    func numberOfSections(in collectionView: UICollectionView) -> Int {
      return 2
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      if section == 0 {
        return 1 // return 1 item in cell (+ cell)
      } else {
        return imageArray.count
      }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0 {
            let firstCell = collectionView.dequeueReusableCell(withReuseIdentifier: "addusercell", for: indexPath) as! AddParticipantsCollectionViewCell
            return firstCell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "userscollectionview", for: indexPath as IndexPath) as! ParticipantsCollectionViewCell
            cell.imageView?.image = self.imageArray[indexPath.row]
            return cell
        }
    }