繁体   English   中英

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

[英]Swift 3 How to display different data for each item in index UICollectionView

我在我的应用程序中创建了一个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文件为每个单元格我不介意设置,但我需要知道如何注册.xib文件显示为单元格,如果你知道你怎么能帮助我?

这是我的手机

细胞的图像

你们是一个巨大的帮助,我想我知道该怎么做

  1. 使用自定义.xib文件制作单元格
  2. 将.xib单元格注册到数组
  3. 将单元格数组加载到collectionView中
  4. 享受自定义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))

希望它能帮到你;)

实现它的方法不止一种。 你的代码似乎是使用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
         }
}

链接到customview的customcell

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM