Ios Swift 3如何在索引UICollectionView中显示每个项目的不同数据

Ios Swift 3如何在索引UICollectionView中显示每个项目的不同数据,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我在我的应用程序中创建了一个UICollectionView,但我不知道如何为每个单元格显示不同的数据,因为我在故事板中设置了相同的图像 我所做的搜索没有产生任何教程,但我知道我在UICollectionViewCell自定义类中自定义了数据 class CollectionViewCell: UICollectionViewCell { //drawing a blank here... } 如果你能帮我,我只想为每个单元格放置一个按钮、两个图像和两个标签(2-3)(大约有12个单元

我在我的应用程序中创建了一个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文件和我一直使用em的cocoa touch类的信息

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

这是我的手机

你们帮了大忙我想我知道怎么做

  • 使用自定义.xib文件生成单元格
  • 将.xib单元格注册到数组
  • 将单元格数组加载到collectionView中
  • 享受定制收藏视图

  • 在控制器中,您必须设置:

        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))
    

    希望对你有帮助;)

    以下是您可以遵循的示例:

    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
    

    实现它的方法不止一种。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
             }
    }
    

    能否请您更详细地说明您的let-dataforcell=dataarrayorwhich[indexPath.row]我不知道understand@E.Huckabee这个答案应该对你的问题有帮助。如果您只想在每个单元格上显示不同的数据,则只需要indexPath.row中的
    字典
    我只想为每个单元格放置一个按钮、两个图像和两个标签(2-3)(大约有12个单元格,我需要每个单元格有不同的数据)
    或者您询问有关自定义单元格的信息。我不清楚我正在做一个角色选择屏幕在我的问题中,我说了12个,但当我更新更多的角色时,这会改变。请检查我编辑的答案以获得一些新信息。你只有一个数组,对吗?然后你尝试了这个答案吗。您需要从数组中获取一个
    indexPath.row
    字典,然后根据它们的索引显示它。尝试了此操作后,它给了我一个错误“对成员“collectionView(uU2;:numberOfItemsInSection:)”的引用不明确”。我想您应该阅读更多关于collectionView()的文档我想做的是做一个角色选择屏幕,每个单元格显示角色肖像和统计数据、价格等。当你点击你想要的角色时,它会推到另一个视图控制器(我知道这是可能的,因为愤怒的小鸟应用程序在它的级别选择菜单中有它)这是否适用于集合视图,或者我是否应该使用其他内容?可能,您应该尝试以下链接:它现在正在工作,只是有一个问题,它说“对成员‘collectionView(uu:numberOfItemsInSection:)的引用不明确”有人知道如何修复吗?如果您希望每个单元格上有这么多不同的数据,最好的建议是创建一个部分。这取决于您使用多个xib或一个xib并重复使用它,就像我在更新的代码中提到的那样。我不会重复使用xib。我需要在collectionView中显示多个.xib,这样更容易