Ios 如何将TableViewCell中的数据作为按钮执行?
我正在创建一个应用程序,其中菜单中列出了Ios 如何将TableViewCell中的数据作为按钮执行?,ios,swift,xcode,uitableview,Ios,Swift,Xcode,Uitableview,我正在创建一个应用程序,其中菜单中列出了TableViewCell。菜单已经在单元格中,但不幸的是,当我单击其中一个菜单时,它没有执行菜单应该执行的操作。例如,我点击了注销标签,它不执行showlogoutDialog。我已经使用了断点,但表中的数据似乎只是纯文本。下图是该表的示例输出。希望您能就这个问题给出一些解决方案。多谢各位 DoctorMenuTableCell.swift class DoctorMenuTableCellTableViewCell: UITableViewCell
TableViewCell
。菜单已经在单元格中,但不幸的是,当我单击其中一个菜单时,它没有执行菜单应该执行的操作。例如,我点击了注销标签
,它不执行showlogoutDialog
。我已经使用了断点,但表中的数据似乎只是纯文本。下图是该表的示例输出。希望您能就这个问题给出一些解决方案。多谢各位
DoctorMenuTableCell.swift
class DoctorMenuTableCellTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
private let menuIdentifier = "MenuCell"
class MoreOptionViewController: UIViewController {
@IBOutlet weak var doctorMenuTableView: UITableView!
}
override func viewDidLoad() {
super.viewDidLoad()
self.doctorMenuTableView.dataSource = self
self.doctorMenuTableView.delegate = self
}
//MARK: Function
func showLogoutDialog() {
//create alert
let alert = UIAlertController(title: "Confirmation", message: "Are you sure you want to logout?", preferredStyle: .alert)
//add the actions (button)
alert.addAction(UIAlertAction(title: "No", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (alert) in
self.logout()
}))
self.present(alert, animated: true, completion: nil)
}
func logout() {
NotificationCenter.default.post(name: Notification.Name(deleteUserDataNotificationName), object: nil)
}
}
extension MoreOptionViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return Menu.More.items.count
default: return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = indexPath.row
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: menuIdentifier, for: indexPath) as! DoctorMenuTableCell
cell.titleLabel.text = Menu.More.items[row].value
if row == 1{
cell.titleLabel.textColor = UIColor.red
cell.accessoryType = .none
}
return cell
default:
return UITableViewCell()
}
}
func tableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0: break
case 1:
switch indexPath.row {
case 0:
let alertController = UIAlertController(title: "Info", message: "No available data", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
case 1: showLogoutDialog()
default: break
}
default: break
}
}
MoreOptionViewController.swift
class DoctorMenuTableCellTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
private let menuIdentifier = "MenuCell"
class MoreOptionViewController: UIViewController {
@IBOutlet weak var doctorMenuTableView: UITableView!
}
override func viewDidLoad() {
super.viewDidLoad()
self.doctorMenuTableView.dataSource = self
self.doctorMenuTableView.delegate = self
}
//MARK: Function
func showLogoutDialog() {
//create alert
let alert = UIAlertController(title: "Confirmation", message: "Are you sure you want to logout?", preferredStyle: .alert)
//add the actions (button)
alert.addAction(UIAlertAction(title: "No", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (alert) in
self.logout()
}))
self.present(alert, animated: true, completion: nil)
}
func logout() {
NotificationCenter.default.post(name: Notification.Name(deleteUserDataNotificationName), object: nil)
}
}
extension MoreOptionViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return Menu.More.items.count
default: return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = indexPath.row
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: menuIdentifier, for: indexPath) as! DoctorMenuTableCell
cell.titleLabel.text = Menu.More.items[row].value
if row == 1{
cell.titleLabel.textColor = UIColor.red
cell.accessoryType = .none
}
return cell
default:
return UITableViewCell()
}
}
func tableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0: break
case 1:
switch indexPath.row {
case 0:
let alertController = UIAlertController(title: "Info", message: "No available data", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
case 1: showLogoutDialog()
default: break
}
default: break
}
}
看看你有多少部分。您只有一个,但在
didSelectRowAt
中,仅当节索引为1时才显示警报,而在您的情况下,索引从来都不是
您只需要检查行是0还是1
func tableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0:
let alertController = UIAlertController(title: "Info", message: "No available data", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
present(alertController, animated: true)
case 1:
showLogoutDialog()
default:
return
}
}
因此,如果只有一个节,还可以更新数据源方法。此外,您还应该处理单元格不在行索引为1的
IndexPath
中的情况,因为单元格已出列
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Menu.More.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: menuIdentifier, for: indexPath) as! DoctorMenuTableCell
let row = indexPath.row
cell.titleLabel.text = Menu.More.items[row].value
cell.titleLabel.textColor = .black
cell.accessoryType = .detailButton
if row == 1
cell.titleLabel.textColor = .red
cell.accessoryType = .none
}
return cell
}
检查您的
Did Select
方法,您的cells
位于Section 0
下,并且Did Select
用于Section 1
同时检查TableView
和Cell
的isUserInteractionEnabled
,并在didSelectRowAt
上添加断点,并检查哪个开关盒正在工作
代码:
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return Menu.More.items.count
default: return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: menuIdentifier, for: indexPath) as! DoctorMenuTableCell
cell.titleLabel.text = Menu.More.items[row].value
if indexPath.row == 1{
cell.titleLabel.textColor = UIColor.red
cell.accessoryType = .none
}
return cell
default:
return UITableViewCell()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 1: break
case 0:
switch indexPath.row {
case 0:
let alertController = UIAlertController(title: "Info", message: "No available data", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
break
case 1: showLogoutDialog()
break
default: break
}
break
default: break
}
}
首先创建扩展,如果UIViewController如下所示 创建一个名为Extension.swift的swift文件或任何您想要的文件
extension UIViewController
{
func showLogoutDialog() {
//create alert
let alert = UIAlertController(title: "Confirmation", message: "Are you sure you want to logout?", preferredStyle: .alert)
//add the actions (button)
alert.addAction(UIAlertAction(title: "No", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (alert) in
self.logout()
}))
self.present(alert, animated: true, completion: nil)
}
func logout() {
print("helllllo")
NotificationCenter.default.post(name: Notification.Name(deleteUserDataNotificationName), object: nil)
}
}
现在在cellForRowAt上调用showLogoutDialog()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 1: break
case 0:
switch indexPath.row {
case 0:
let alertController = UIAlertController(title: "Info", message: "your data", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
break
case 1: showLogoutDialog()
break
default: break
}
break
default: break
}
你几乎完成了,你只是把
开关
的情况弄糟了。检查我下面的答案,了解您做错了什么。有时,特别是当您在ViewController中使用tableView而不是TableViewController时,您需要添加numberOfSection=1或(您需要多少)。在这之后,它开始按预期工作嗨,谢谢你的帮助,我试图修改我的代码,我使用了你的答案,但仍然不起作用。我试图点击两个菜单(设置和注销),但它仍然没有执行:(我不知道我的代码哪一部分出错了。)谢谢你的帮助,我也尝试了你的答案,但仍然不起作用wrong@Titus请检查我的更新答案,并告诉我您是否仍有任何问题。能否将屏幕记录添加为gif
,以便更好地调试?如果可以,我会尝试n添加屏幕记录,在哪个部分?“选择”功能