I created a collection of views and a custom cell via xib
.
There is only a Label
on the cell.
I have to enter the text through the TextField
, fill in the array with this text, and put it in the cell.
I need to create a cell after each text entry in the TextField
.
I haven't worked with collections yet and don't quite understand how it works.
That's all I have for now
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
}
}
Error:
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)
To ask the collection view to update itself you need to call its reloadData()
method.
So your IBAction
should look like this:
@IBAction func inputNameField(_ sender: UITextField) {
playersNames.append(nameTextFIeld.text!)
collectionPlayersView.reloadData()
}
您需要在您的 xib 文件中设置单元格标识符“playersCell”,并在viewDidLoad
设置collectionView.delegate = self
和collectionView.dataSource = self
You need to mention your xib file name instead of "playerCellId"
let nibPlayersCell = UINib(nibName: playerCellId, bundle: nil)
It requires nibName = "YourNibName"
If your nib name is CollectionPlayersCell, then this should be like this -
let nibPlayersCell = UINib(nibName: "CollectionPlayersCell", bundle: nil)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.