【问题标题】:How to execute data inside TableViewCell as a button?如何将 TableViewCell 中的数据作为按钮执行?
【发布时间】:2019-03-27 06:41:32
【问题描述】:

我正在创建一个应用程序,其中的菜单在TableViewCell 中列出。菜单已经在单元格内,但不幸的是,当我单击其中一个菜单时,它没有执行菜单应该执行的操作。例如,我点击了logout label,它没有执行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
}

MoreOptionViewController.swift

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

    }

}

【问题讨论】:

  • 你差不多完成了,你只是搞砸了Switch 案例。在下面查看我的答案以了解您做错了什么。
  • 有时,尤其是当您在 ViewController 中使用 tableView 而不是 TableViewController 时,您需要添加 numberOfSection = 1 或(您需要多少)。之后它开始按预期工作

标签: ios swift xcode uitableview


【解决方案1】:

检查您的Did Select 方法,您的cellsSection 0Did Select 下为section 1 工作。

同时检查isUserInteractionEnabled 中的TableViewCell 并在didSelectRowAt 上添加断点并检查哪个switch case 正在工作。

代码:

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

     }

  }

【讨论】:

  • 您好也感谢您的帮助,我也尝试了您的答案,但仍然无法正常工作。我不知道哪一部分错了
  • @Titus 检查我的更新答案,如果您仍有任何问题,请告诉我。
  • 您能否将screen record 添加为gif,以便更好地调试?
  • 我试试能不能加录屏,在哪个部分?
  • “选择”功能
【解决方案2】:

看看你有多少部分。您只有一个,但在 didSelectRowAt 中,如果 section index 为 1,您会显示警报,而在您的情况下 index 永远不会。

你只需要检查 row 是 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
    }
}

所以,如果你只有一个部分,你也可以更新数据源方法。此外,您应该处理单元格不在 IndexPath 中且行索引为 1 的情况,因为单元格已出列

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
}

【讨论】:

  • 您好,感谢您的帮助,我尝试修改我的代码并使用了您的答案,但仍然无法正常工作。我试图点击两个菜单(设置和注销)它仍然没有执行。 :( 我很困惑我的代码哪一部分有错误
【解决方案3】:

如果 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

     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多