how i can create TableView with three sections, but in first section I want to display CollectionView with horizontal scroll, in second section I want to display only two cells with text and in third section I want to display only three cells with text.
I have only part of code, but I think he is not correct.
class SettingsScheduleAndPricesViewController: UITableViewController {
var dayOfWeek = [NSLocalizableMo,
NSLocalizableTu,
NSLocalizableWe,
NSLocalizableTh,
NSLocalizableFr,
NSLocalizableSa,
NSLocalizableSu]
let sectionTitle = ["Day of week", "Second section", "Third section"]
let secondRowText = ["First row", "Second row"]
let thirdRowText = ["Row one", "Row two", "Row three"]
override func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitle.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: return 1
case 1: return 2
default: return 3
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if indexPath.section == 0 {
} else if indexPath.section == 1 {
cell.textLabel?.text = secondRowText[indexPath.row]
} else {
cell.textLabel?.text = thirdRowText[indexPath.row]
}
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionTitle[section]
}
}
// MARK: - CollectionView
extension SettingsScheduleAndPricesViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dayOfWeek.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dayOfWeekCell", for: indexPath) as! SettingsDayOfWeekCell
cell.dayOfWeekLabel.text = dayOfWeek[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width / 7.0
let height = width
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets.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
}
}
Pls help how I can do it?
You can do this very easily, There are few things which are missed and misplaced in your code
I will try to explain how you can do it with having some changes in your code.
Coding Example:
In Your View Controller:
Create One new class for DayofweekCell and setup function in it. In that class you need to add your collection view delegate and data source methods.
Add your collection view in your DayofweekCell and set delegate and data source there. define your data source there only. you can then use delegation to delegate your selection of collection view item to view controller. I hope this will give you an idea to solve your problem ;)
Issue here is, that you always dequeue the same cell in
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
which in your case is your CollectionView cell.Try:
You have to register a UITableViewCell for
default cell
before. The same way you registered your CollectionView cell.