【问题标题】:making cells in collection view dynamic and add images to them使集合视图中的单元格动态并向其中添加图像
【发布时间】:2021-10-30 09:40:24
【问题描述】:

我要上传不同用户类型的图片。因此,对于用户类型 1..我想创建一个集合视图,显示 3 个单元格,其中包含 3 个添加和显示图片的选项,对于其他用户,每个单元格 2 个。如上图,为 userType = 3 创建了 2 个单元格。到目前为止,我能够根据用户创建所需数量的单元格,但无法上传其中任何一个单元格中的图片。我已经有单独的 imagePicker 委托方法可以将图片上传到其他地方,但那是静态图片。我想在这些动态创建的单元格中上传图片。因此,如果用户 = 3,则返回 2 个包含图像视图和按钮(文档)的单元格以添加图片。到目前为止,这是我的代码:

let reuseIdentifier = "cell"
        
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            let userType = typeOfUser //typeOfUser = 1,2,3
            var cellNumber = 0
            if userType == "2" {
                cellNumber = 3 //to return 3 cells for userType 2
            } else {
                cellNumber = 2 //to return 2 cells for userType 1 and 3
            }
            return cellNumber
        }
        
 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! DocumentCollectionViewCell
            if userType == "2" {
                for index in 0 ..< 3 {  // for user = "2", 3 cells required so I used forloop < 3  
                   let imageData:NSData = cell.userPic.image!.jpegData(compressionQuality: 60)! as NSData //compress image
                   let base64 = imageData.base64EncodedString(options: .lineLength64Characters)   //store image in base64
    
               //code below to pass base64 to image view and give it some id(stuck at creating logic for that)
               
            }  
         }
       return cell
}

此代码确实返回正确数量的单元格,其中包含 3 个空图像视图,每个视图下方添加图片按钮为 typeOfUser = "2",但如何使每个 imageView 下方的按钮一次添加 1 张图片到一个单元格?让每个按钮使用该 imagePicker 委托并打开照片库以在“n”个单元格中为各自的用户类型添加图片?

就像我如何通过单击第一个图像下方的文档按钮然后第二个图像以及如果其他用户通过单击创建的每个图像视图下方的按钮添加尽可能多的图像来添加图片一样,无论有多少单元格是否返回?

简而言之,这就是我想要的:

  1. 对于用户 = 3
  2. 如上图返回 2 个单元格
  3. 点击每张图片下方的文档按钮添加图片
  4. 对于其他用户,对不同数量的单元格执行相同操作。

【问题讨论】:

  • 我希望我不是在说显而易见的,但 cellForItemAt 函数返回一个空(未填充)单元格。也不清楚for 循环对base64 数据做了什么。最后,indexPath 没有用于检索任何相关数据 - 这表明您应该填充和返回哪个单元格。也许还有更多代码?你能更新和澄清这个问题吗? This Tutorial 可能会有所帮助。
  • @Jay 我编辑了问题。
  • 仍然不确定问题是否明确,但这 - 返回 2 个像上图一样的单元格 - 听起来很时髦。该函数返回 1 个单元格,而不是 2 个单元格。因此将为每个部分中的每个 indexPath 调用它。例如; this this numberOfItemsInSection 返回 3 因为有三个项目然后 this collectionView:cellForItemAt 将被调用 3 次;每个单元格一次。每个单元格都需要填充图像或其他内容并返回。

标签: ios swift xcode uikit collectionview


【解决方案1】:

你可以通过委托来实现。

  1. 创建协议:
protocol ButtonDelegate  {
    // Define expected delegate functions
    func cellButtonClicked(tag: Int, indexPath: IndexPath)
}
  1. 在您要采取行动的课堂上确认此协议:
extension ViewController: ButtonDelegate {
    
func cellButtonClicked(tag: Int, indexPath: IndexPath) {
    
//implement Logic to open ImageLibrary, you have indexPath available here
}
}
  1. PhotoCell 内部创建委托实例和委托方法所需的信息:
class PhotoCell... {
    var cellButtonDelegate:  ButtonDelegate?
    var indexPath: IndexPath = IndexPath()
}
  1. ViewController:func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell 中添加以下行:
cell.indexPath = indexPath
cell.ImageButton.tag = 0 // tag to identify userNumber
cell.cellButtonDelegate = self
  1. PhotoCell 内的按钮操作中,通知委托函数:
cellButtonDelegate.cellButtonClicked(tag: sender.tag, indexPath: self.indexPath)

【讨论】:

    猜你喜欢
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    相关资源
    最近更新 更多