Reputation: 4885
I am declaring a UICollectionView
and its corresponding cell programatically. After writing out the vanilla setup, I find that I cannot configure the size of the cell. Regardless of whether i place the logic, or what dimension, the cells (in red) appear to be some default square (and small) size. A screen capture is at the end of this post.
For reference, the code is below:
setupView(){}
// collection view for screens
let screenHeight : CGFloat = CGFloat(height/6)
let screenWidth = screenHeight * ( width/height )
let offset_top_screens : CGFloat = height - rowHeight - screenHeight
let screenRow = UICollectionView(
frame: CGRect(x: 0, y: offset_top_screens, width: width, height: screenHeight)
, collectionViewLayout: UICollectionViewFlowLayout()
)
//screenRow.translatesAutoresizingMaskIntoConstraints = false
screenRow.backgroundColor = Color.grayTertiary
// set up collection view delegates
screenRow.dataSource = self
screenRow.alwaysBounceHorizontal = true
screenRow.register(ScreenItem.self, forCellWithReuseIdentifier: ScreenItem.identifier) // register w/ storyboard
self.view.addSubview(screenRow)
self.screenRow = screenRow
self.view.bringSubviewToFront(screenRow)
self.screenHeight = screenHeight
self.screenWidth = screenWidth
// end collection view screen
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let row = indexPath.row
let user = self.dataSource[row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ScreenItem", for: indexPath) as! ScreenItem
/*
cell.uuid = user.uuid
cell.delegate = self
cell.mediaSource = ... what is the pipe here?
*/
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 45, height:40) // changing these values do not change anything
// return CGSize(width: 90, height: collectionView.bounds.height) <- this does nothing
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //.zero
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
// somewhere in a different file
/*
@Use: display user's screen
*/
class ScreenItem: UICollectionViewCell {
static var identifier: String = "ScreenItem"
weak var textLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = Color.red
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func prepareForReuse() {
super.prepareForReuse()
self.reset()
}
func reset() {
// @TOOD: reset things here
}
}
__ update __ parent class definition
class CallController:
UIViewController
, UICollectionViewDataSource
, UICollectionViewDelegate
Upvotes: 0
Views: 912
Reputation: 2639
You don't seem to set a delegate. If you are using the UICollectionViewFlowLayout
then your UIViewController
should implement the UICollectionViewDelegateFlowLayout
protocol.
UICollectionViewDelegateFlowLayout
Does your UIViewController
instance already implement this protocol? Did you set the delegate
property?
Upvotes: 2