У меня есть UICollectionView внутри моего UITableViewCell, и я хочу отобразить несколько ячеек коллекции. Он отлично работает, если numberOfItemsInSection больше 1. Но если я только хочу вернуть 1 Item, он входит в метод numberOfItemsInSection и возвращает 1. Пока все хорошо, но метод cellForRowAt не вводится, когда у меня только 1 Item. Пока у меня есть 2 или более элементов, все работает отлично.
В настоящее время я работаю с раскадровкой, но я также пытался создать ячейки программно, но это все равно не сработало.
Класс ячейки табличного представления
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 SingleTaskCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var label: UILabel!
func setUpViews() {
containerView.layer.cornerRadius = SIZE.CORNER_RADIUS_1
}
}
Класс задачи
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
}
}