【问题标题】:Swift 3 How to display different data for each item in index UICollectionViewSwift 3 如何为索引 UICollectionView 中的每个项目显示不同的数据
【发布时间】:2018-02-14 15:55:13
【问题描述】:

我在我的应用程序中创建了一个 UICollectionView,但我不知道如何为每个单元格显示不同的数据,我遇到了我在情节提要中设置的相同图像

我所做的搜索没有产生任何教程,但我知道我在 UICollectionViewCell 自定义类中自定义了我的数据

class CollectionViewCell: UICollectionViewCell {
    //drawing a blank here...
}

如果你能帮助我,我只想为每个单元格放置一个按钮、两个图像和几个标签 (2-3)(大约有 12 个单元格,我需要它们为每个单元格提供不同的数据,因为我我正在尝试制作角色选择屏幕)

我目前的设置

class CharacterViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var array = [UIImage(named: "ideal image"),UIImage(named: "ideal image"),UIImage(named: "ideal image")]

override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return array.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FirstCollectionViewCell", for: indexPath) as! FirstCollectionViewCell

    return cell
}

}

我能够显示三个完全相同的图像,但我需要单元格具有单独的可自定义数据

我看到的一个网站建议为每个单元格使用 .xib 文件,我不介意设置,但如果您知道如何帮助我,我需要知道如何注册 .xib 文件以显示为单元格?

这是我的牢房

你们帮了我很大的忙,我想我知道该怎么做

  1. 使用自定义 .xib 文件制作单元格
  2. 将 .xib 单元格注册到数组中
  3. 将单元格数组加载到 collectionView 中
  4. 享受自定义collectionView

【问题讨论】:

  • 也许,您应该尝试以下链接:stackoverflow.com/questions/37315466/…
  • 它现在工作的只是一个问题,它说“对成员'collectionView(_:numberOfItemsInSection:)'的模糊引用”有人知道如何解决吗?

标签: ios swift uicollectionview


【解决方案1】:

实现它的方法不止一种。 Bu您的代码似乎使用了customcell 它的代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell

      cell.backgroundColor = UIColor.white

         if indexPath.section == 0 {

      cell.imageView.image = image
                }
         if indexPath.section == 1 {

      cell.imageView.image = image
                }
      return cell
 }

override func numberOfSections(in collectionView: UICollectionView) -> Int {
              return 2 // whatever you want
}

         //2
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

         return searches[section].searchResults.count // if each section have same cell

         if section == 0 {
                return 2
            } else if section == 1 {
                return 3
         }
}

Link to customcell for collectionview

【讨论】:

  • 如果您希望每个单元格上有如此多不同的数据,最好的建议是创建部分。
  • 这取决于你是否使用多个 xib 或一个 xib 并像我在更新的代码中提到的那样重用它
  • 我不会重用 xib,我需要在 collectionView 中显示多个 .xib 会更容易
【解决方案2】:

这是一个您可以遵循的示例:

class CollectionViewCell: UICollectionViewCell {

let myButton : UIButton = {
    let btn = UIButton(type: .system)
    btn.translatesAutoresizingMaskIntoConstraints = false
    btn.setTitle("My button", for: .normal)
    return btn
}()

override init(frame: CGRect){
    super.init(frame: frame)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupViews(){
    // Add subViews to your custom cell
    addSubview(myButton)
    myButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    myButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    myButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
    myButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
}

}

下一步,你必须像这样在 viewDidLoad 中注册单元类:

collectionView.register(CollectionViewCell.self, forCellReuseIdentifier: "customCell")

最后一步,在你定义的 cellForItemAt 函数中:

let cell = collectionView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomViewCell
        return cell

【讨论】:

  • 试过这个它给了我一个错误“对成员'collectionView(_:numberOfItemsInSection:)'的模糊引用”
  • 我认为你应该阅读更多关于 collectionView 的文档 (stackoverflow.com/questions/17856055/…)
  • 我想做的是制作一个角色选择屏幕,每个单元格显示角色肖像和统计数据、价格等,当您点击您想要的角色时,它会推送到不同的视图控制器(我知道它是可能的,因为应用程序在其级别选择菜单中有愤怒的小鸟)这可能与集合视图还是我应该使用其他东西
【解决方案3】:

在你的控制器中你必须设置这个:

    Class yourController : UIViewController,UICollectionViewDelegate, UICollectionViewDataSource{...
 //at some point 
yourCollectionView.delegate = self
yourCollectionView.dataSource = self

然后你包含这个方法,你就可以在每个单元格中加载你想要的数据:

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: yourCell = collection.dequeueReusableCell(withReuseIdentifier: nameCelda, for: indexPath) as! yourCell
//dataArrayOrWhatever -> An array of data for each row
        let dataForCelll =  dataArrayOrWhatever[indexPath.row]
//functions defined on your yourCell.Swift
        cell.setImage(imageName: someMethodWithDataForImage(data: dataForCell))
        cell.setTitle(title: someMethodWithDataForTitle(data: dataForCell))

希望对你有所帮助;)

【讨论】:

  • 能否请您更具体地说明您的 let dataforcell = dataArrayOrWhatever[indexPath.row] 我不明白
  • @E.Huckabee 这个答案应该对您的问题有所帮助。如果您只想在每个单元格上显示不同的数据,您只需要来自 indexPath.row 的 DictionaryI just want to put a button, two images and a couple labels (2-3) for each cell (there will be about 12 cells and I need them to have different data for each) 或者您询问自定义单元格。我不清楚
  • 我正在制作一个角色选择屏幕
  • 在我的问题中,我说的是 12,但是当我更新更多要玩的角色时,情况会改变,请查看我编辑的答案以获取一些新信息
  • 你只有一个数组是对的,那你试试这个答案吗? .您需要从数组中获取indexPath.row 的字典,然后根据它们的索引显示它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2022-01-14
  • 2016-05-26
相关资源
最近更新 更多