Reputation: 123
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)
Upvotes: 0
Views: 870
Reputation: 492
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)
Upvotes: 1
Reputation: 1084
You need to set the cell identifier "playersCell" in your xib file
and set the collectionView.delegate = self
and collectionView.dataSource = self
in viewDidLoad
Upvotes: 1
Reputation: 7367
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()
}
Upvotes: 1