gpt4 book ai didi

ios - 将单元格添加到 Collection View 。 swift

转载 作者:行者123 更新时间:2023-11-28 19:25:26 25 4
gpt4 key购买 nike

我通过 xib 创建了一个 View 集合和一个自定义单元格。

单元格上只有一个Label

我必须通过 TextField 输入文本,用该文本填充数组,然后将其放入单元格中。

我需要在 TextField 中的每个文本条目之后创建一个单元格。

我还没有接触过集合,也不太明白它是如何工作的。

这就是我的全部

CollectionCell.swift:

import UIKit
class CollectionPlayersCell: UICollectionViewCell {

@IBOutlet weak var playerCell: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
//someCode
}

}

ViewController.swift

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var collectionPlayersView: UICollectionView!
@IBOutlet weak var nameTextFIeld: UITextField!
let playerCellId = "playersCell"
var playersNames = [String]()

@IBAction func inputNameField(_ sender: UITextField) {

playersNames.append(nameTextFIeld.text!)

}
override func viewDidLoad() {
super.viewDidLoad()

let nibPlayersCell = UINib(nibName: playerCellId, bundle: nil)
colecctionPlayersView.register(nibPlayersCell, forCellWithReuseIdentifier: playerCellId)
notificationKeyboard()

}


extension ViewController: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: playerCellId, for: indexPath) as! CollectionPlayersCell
cell.playerCell.text = playersNames[indexPath.row]
return cell
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

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


}

错误:

2019-12-06 19:34:45.823972+0200 Games[3143:174982] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle

< /Users/macbookpro/Library/Developer/CoreSimulator/Devices/0DF2A96E-E367-41D3-BE33-4F3FADE3EFCE/data/Containers/Bundle/Application/2818E8FD-23FE-4E86-925B-EC3F58EF6BFC/Games.app > (loaded)' with name 'playersCell''

libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

最佳答案

您需要提及您的 xib 文件名而不是“playerCellId”

let nibPlayersCell = UINib(nibName: playerCellId, bundle: nil)

需要 nibName = "YourNibName"

如果你的 nib 名称是 CollectionPlayersCell,那么这应该是这样的 -

let nibPlayersCell = UINib(nibName: "CollectionPlayersCell", bundle: nil)

关于ios - 将单元格添加到 Collection View 。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59217422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com