【发布时间】:2020-09-16 07:11:33
【问题描述】:
我想在 iOS 的 instagram 应用程序中构建用户个人资料页面,我使用了带有嵌套 collectionView 的 tableView,但它对我来说无法正常工作,是否有任何建议或某些方法可以考虑,我只对 Profile Header 感兴趣(个人资料图片和简历)和带有水平滚动部分的照片网格。
目前我实现了这种行为(忽略部分的水平滚动):
tableView 和 collectionView 不会像单独滚动的单个内容或单个页面那样一起滚动,如果 collectionView scrollingisEnabled = false 和 tableViewCell 高度很大,它可以正常工作,但 collectionView 会一次加载整个单元格
故事板视图层次结构:
源代码:
class ViewController: UITableViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(SectionHeader.self, forHeaderFooterViewReuseIdentifier: "h")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.size.height - tableView.tableHeaderView!.frame.height
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let x = tableView.dequeueReusableHeaderFooterView(withIdentifier: "h") as! SectionHeader
x.contentView.backgroundColor = .green
x.addSections(sectionNames: [ "A", "B", "C", "D" ])
x.segment.addTarget(self, action: #selector(handleSegmentSectionChange(segmentControl:)), for: .valueChanged)
return x
}
@objc
func handleSegmentSectionChange(segmentControl: UISegmentedControl) {
print("load section")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "c", for: indexPath)
let label = cell.viewWithTag(10) as! UILabel
label.text = "\(indexPath)"
print("getting cell at \(indexPath)")
cell.backgroundColor = .orange
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width , height: 80)
}
}
谢谢。
【问题讨论】:
-
欢迎来到 SO。这个问题太宽泛了,我们无法以任何有意义的方式回答。展示您尝试过的内容以及遇到的具体问题。
-
@Marcelo,请在创建此配置文件时描述您的问题。这不是给你一些项目示例的 Github。
-
@King.lbt 我不是要一个项目老兄,我只是需要一个建议或想法来帮助我完成它
标签: ios swift uitableview uiscrollview uikit