【发布时间】:2019-06-15 22:53:21
【问题描述】:
我如何创建具有三个部分的 TableView,但在第一部分我想显示 CollectionView 水平滚动,在第二部分我只想显示两个单元格文本,在第三部分我只想显示三个带文本的单元格。
我只有部分代码,但我认为他不正确。
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
}
}
请帮助我怎么做?
【问题讨论】:
-
显然缺少一些代码来解释我们在屏幕截图中看到的内容。我想问题在于您将集合视图附加到第一行的方式。否则,我在表委托代码中看不到任何明显的内容。
-
@Rafouille 是的,他没有在表格视图中添加一些方法,例如行方法的高度。并且他需要将集合视图的委托和数据源添加到第 1 节单元格类中。
标签: ios swift xcode uitableview uicollectionview