【发布时间】:2021-05-01 03:04:58
【问题描述】:
大家好 :) 我的自定义单元格的自动布局有点问题,我有 5 个自定义单元格都已加载,但问题是无论我做什么,它们都会相互覆盖,我试图找到它的问题,但我似乎没有找到它:\ 很想得到帮助。 顺便说一句:我没有使用任何故事板。
这是我的 tableView 的代码:
import UIKit
import PanModal
class FilterTableViewController: UITableViewController, PanModalPresentable {
var panScrollable: UIScrollView? {
return tableView
}
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
// registerTableViewCells()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// tableView.frame = view.bounds
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// MARK: - View Configurations
func setupTableView() {
tableView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
tableView.separatorStyle = .singleLine
tableView.isScrollEnabled = false // We don't want it to scroll
tableView.allowsSelection = false
tableView.estimatedRowHeight = 160
tableView.rowHeight = UITableView.automaticDimension
tableView.rowHeight = 80
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
private func registerTableViewCells() {
let textFieldCell = UINib(nibName: "byActivityCell",
bundle: nil)
self.tableView.register(textFieldCell,
forCellReuseIdentifier: "cell")
}
// MARK: - UITableViewDataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let byActivityCell = UINib(nibName: "byActivityCell",bundle: nil)
self.tableView.register(byActivityCell,forCellReuseIdentifier: "byActivityCell")
return tableView.dequeueReusableCell(withIdentifier: "byActivityCell", for: indexPath) as! byActivityCell
case 1:
let byTypeCell = UINib(nibName: "ByType",bundle: nil)
self.tableView.register(byTypeCell,forCellReuseIdentifier: "byTypeCell")
return tableView.dequeueReusableCell(withIdentifier: "byTypeCell", for: indexPath) as! ByType
case 2:
let byHashtagsCell = UINib(nibName: "ByHashtags",bundle: nil)
self.tableView.register(byHashtagsCell,forCellReuseIdentifier: "byHashtagsCell")
return tableView.dequeueReusableCell(withIdentifier: "byHashtagsCell", for: indexPath) as! ByHashtags
case 3:
let byDatesCell = UINib(nibName: "DatesCell",bundle: nil)
self.tableView.register(byDatesCell,forCellReuseIdentifier: "byDatesCell")
return tableView.dequeueReusableCell(withIdentifier: "byDatesCell", for: indexPath) as! DatesCell
case 4:
let byAlbumCell = UINib(nibName: "AlbumCell",bundle: nil)
self.tableView.register(byAlbumCell,forCellReuseIdentifier: "byAlbumCell")
return tableView.dequeueReusableCell(withIdentifier: "byAlbumCell", for: indexPath) as! AlbumCell
default:
return UITableViewCell()
}
}
// MARK: - footer Methods:
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return getfooterView()
}
func getfooterView() -> UIView
{
let Header = UIView(frame: CGRect(x: 0, y: 0, width: Double(self.tableView.frame.size.width), height: 45))
Header.backgroundColor = UIColor(named: "#2AF8AC")
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: Header.frame.size.width-10, height: Header.frame.size.height)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = 12
button.layer.masksToBounds = true
button.setTitle("Apply Filters", for: .normal)
button.setTitleColor(.white, for: .normal)
// button.addTarget(self, action: nil, for: UIControl.Event.touchUpInside)
Header.addSubview(button)
Header.bringSubviewToFront(button)
return Header
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 45
}
}
如下所示:Link.
【问题讨论】:
标签: ios swift autolayout tableview custom-cell