【发布时间】:2021-06-10 21:21:43
【问题描述】:
我有一个包含表格视图的视图控制器。在这个视图控制器中,我还设置了一个浮动面板。但是,由于某种原因,浮动面板没有出现。我知道 ContentViewController 正在工作,所以我没有包含该代码。错误在此代码中的某处,我找不到它。这就像 FloatingPanel 只是没有被添加到视图中。
import Foundation
import UIKit
import FloatingPanel
class Section {
let title: String
let options: [String]
var isOpened: Bool = false
init(title: String,
options: [String],
isOpened: Bool
) {
self.title = title
self.options = options
self.isOpened = isOpened
}
}
class PicsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, FloatingPanelControllerDelegate {
@IBOutlet weak var addButton: UIButton!
private let picsTableView: UITableView = {
let picsTableView = UITableView()
picsTableView.register(UITableViewCell.self,
forCellReuseIdentifier: "cell")
return picsTableView
}()
private var sections = [Section]()
let backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
/*------------START FLOATING PANEL-------------*/
var fpc: FloatingPanelController!
override func viewDidLoad() {
super.viewDidLoad()
print("PicsViewController LAUNCHED!!")
fpc = FloatingPanelController()
fpc.delegate = self
// guard let contentVC = storyboard?.instantiateViewController(identifier: "fpc_content") as? ContentViewController else {
// print("Failed to instantiate storyboard")
// return
// }
let contentVC = ContentViewController()
fpc.set(contentViewController: contentVC)
// fpc.track(scrollView: contentVC.collectionView)
fpc.addPanel(toParent: self)
/*------------END FLOATING PANEL-------------*/
// Set up models
picsTableView.delegate = self
picsTableView.dataSource = self
picsTableView.frame = view.bounds
picsTableView.backgroundColor = backgroundColor
view.addSubview(picsTableView)
sections = [
Section(title: "Section 1", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
Section(title: "Section 2", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
Section(title: "Section 3", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
Section(title: "Section 4", options: ["Test 1", "Test 2", "Test 3"], isOpened: false)
]
// get rid of extra table view cells
picsTableView.tableFooterView = UIView()
// customize button
addButton.layer.cornerRadius = 0.5 * addButton.bounds.size.width
addButton.clipsToBounds = true
addButton.backgroundColor = .red
addButton.tintColor = .white
addButton.setImage(#imageLiteral(resourceName: "plus-5-512"), for: .normal)
view.bringSubviewToFront(addButton)
}
@IBAction func addButtonTapped(_ sender: Any) {
print("Add button tapped")
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let section = sections[section]
if section.isOpened {
return section.options.count + 1
} else {
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = picsTableView.dequeueReusableCell(
withIdentifier: "cell",
for: indexPath
)
cell.backgroundColor = .clear
cell.textLabel?.textColor = .black
if indexPath.row == 0 {
cell.textLabel?.text = sections[indexPath.section].title
} else {
cell.textLabel?.text = sections[indexPath.section].options[indexPath.row - 1]
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
picsTableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0 {
sections[indexPath.section].isOpened = !sections[indexPath.section].isOpened
picsTableView.reloadSections([indexPath.section], with: .none)
} else {
print("Tapped sub cell \(indexPath.row)")
}
}
}
【问题讨论】:
-
您尝试过我发布的答案吗?它奏效了吗?你还有什么问题吗?
标签: ios swift xcode panel floating