Reputation: 1
I have a UICollectionView inside my UITableViewCell and I want to display multiple Collection Cells. It works perfectly if the numberOfItemsInSection is more than 1. But if I only want to return 1 Item it enters the numberOfItemsInSection method and returns 1. So far so good, but the cellForRowAt method is not entered when I only have 1 Item. As long as I have 2 or more Items it works perfectly.
I am currently working with the storyboard but I also tried creating the cells programmatically but it still didn't work.
Table View Cell Class
class TaskTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var taskCollectionView: UICollectionView!
@IBOutlet weak var customerLabel: UILabel!
@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var colorView: UIView!
@IBOutlet weak var shadowView: UIView!
private var currentTask: Task!
func initialize(task: Task) {
currentTask = task
taskCollectionView.delegate = self
taskCollectionView.dataSource = self
taskCollectionView.allowsSelection = false
taskCollectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return currentTask.tasks.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Not entering if numberOfItems is 1
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SingleTaskCell", for: indexPath) as! SingleTaskCollectionViewCell
cell.setUpViews()
return cell
}
}
CollectionViewCell Class
class SingleTaskCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var label: UILabel!
func setUpViews() {
containerView.layer.cornerRadius = SIZE.CORNER_RADIUS_1
}
}
Task Class
class Task {
var id: Int!
var tasks: [String]
var date: Date?
var customer: Customer
var isFinished: Bool
var isAppointment: Bool
init(id: Int? = nil, tasks: [String], date: String? = nil, formattedDate: Date? = nil, customer: Customer, isFinished: Bool = false, isAppointment: Bool = false) {
if id != nil {
self.id = id
}
self.tasks = tasks
if formattedDate != nil {
self.date = formattedDate!
} else {
if date != nil {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
self.date = dateFormatterGet.date(from: date!)!
} else {
self.date = nil
}
}
self.customer = customer
self.isFinished = isFinished
self.isAppointment = isAppointment
}
}
Upvotes: 0
Views: 488
Reputation: 5
CellforRow at:indexPath method won't be called if the size or position of the collectionView is changed before reload, make sure you're not doing that.
Upvotes: 0
Reputation: 1
EDIT: I fixed it. The mistake was with the autoresizing of the cells. Because of that if I had only 1 cell it wouldn't display in the visible region and that's why cellForItemAt wasn't called.
I fixed it by changing up some settings in the storyboard and calculate the size of the cells in the sizeForItemAt method.
Upvotes: 0