【发布时间】:2019-07-20 22:15:38
【问题描述】:
我两周前开始学习 Swift。通过一些课程和 youtube 视频,我学习了 uitableview 的基础知识,但我不知道如何像按钮一样使用特定的表格视图单元格。
我想做什么?
我正在尝试为我的项目使用侧边栏菜单 (https://github.com/yannickl/FlowingMenu),一切都很酷,并且工作正常,而不是使用单元格。
在每个基本教程中,人们都展示了如何将 segue 与 uitableview 一起使用,但他们只使用 2 页并且独立于单元格值,无论单元格中的什么文本它都为第二个控制器调用 segue。
我想使用带有单元格的侧边栏菜单,如果我的单元格值为“主菜单”,我想执行“toMainMenu”segue。
我只知道将单元格与数组一起使用,不知道有没有其他方法可以处理单元格,我的主要目标是特定于单元格的 segue。
我看了很多教程,但找不到对我有用的东西。
import UIKit
导入流动菜单
类 SideBarMenu: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var surnameLabel: UILabel!
@IBOutlet weak var topBar: UINavigationBar!
@IBOutlet weak var backButtonItem: UIBarButtonItem!
@IBOutlet weak var userTableView: UITableView!
@IBOutlet weak var profilePic: UIImageView!
let CellName = "Menu"
let mainColor = UIColor(hex: 0xC4ABAA)
var sections = ["Main Menu", "Settings", "Profile"]
override func viewDidLoad() {
super.viewDidLoad()
userTableView.delegate = self
userTableView.dataSource = self
nameLabel.text = Helper.name
surnameLabel.text = Helper.surname
topBar.tintColor = .black
topBar.barTintColor = mainColor
topBar.titleTextAttributes = [
NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 22)!,
NSAttributedString.Key.foregroundColor: UIColor.black
]
userTableView.backgroundColor = mainColor
view.backgroundColor = mainColor
}
// MARK: - Managing the Status Bar
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// MARK: - UITableView DataSource Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = sections[indexPath.row]
cell.contentView.backgroundColor = mainColor
return cell
}
// MARK: - UITableView Delegate Methods
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Select row at")
}
@IBAction func profileButton(_ sender: Any) {
performSegue(withIdentifier: "toProfile", sender: nil)
}
}
我想在点击“设置”单元格时执行“toSettings”转场。
【问题讨论】:
-
在此处发布一些代码,指出您有疑问或遇到问题的地方会很有帮助。
-
@BobHy 添加和编辑。
标签: ios swift uitableview segue cell