簡體   English   中英

在 uitableview 部分選擇行

[英]selecting row in uitableview section

大家好,我想問一下我想在tableview部分選擇行,當我選擇部分行時,它會顯示一個alertController。 但它沒有顯示,我在第 0 部分有另一種方法並且它有效,但為什么它在我想選擇的部分中不起作用。 你能幫助我嗎?

func numberOfSections(in tableView: UITableView) -> Int {
        return 5
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            let selectSchoolVC = SelectSchoolPopUpVC()
            selectSchoolVC.modalPresentationStyle = .fullScreen
            selectSchoolVC.modalTransitionStyle   = .crossDissolve
            present(selectSchoolVC, animated: true, completion: nil)
        } else if indexPath.section == 1 {

        } else if indexPath.section == 2 {

        } else if indexPath.section == 3 {

        }

        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Log Out", style: .destructive, handler: { (_) in

            ProfileServices.shared.signOutUser()

            let signInVC = SigninViewController()
            let navController = UINavigationController(rootViewController: signInVC)
            self.present(navController, animated: true)
        }))

        tableView.deselectRow(at: indexPath, animated: true)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "selectId", for: indexPath) as! SelectSchoolCell

            return cell

        } else if indexPath.section == 1 {
            return UITableViewCell()
        } else if indexPath.section == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BadgedCell", for: indexPath) as! BadgeCell
            cell.accessoryType = .disclosureIndicator

            let item = infoArray[indexPath.row]
            cell.nameLbl.text       = item["title"]
            cell.badgeString        = item["badge"] ?? ""
            cell.badgeTextColor     = .white
            cell.badgeColor         = .red

            return cell
        } else if indexPath.section == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "helpId", for: indexPath)
            let helpsArray = helpArray[indexPath.row]

            cell.textLabel?.text = helpsArray
            cell.textLabel?.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
            cell.textLabel?.font = UIFont(name: "NunitoSans-SemiBold", size: 16)
            cell.accessoryType = .disclosureIndicator
            return cell
        }

        let cell = tableView.dequeueReusableCell(withIdentifier: "signOutId", for: indexPath)
        return cell
    }

你從來沒有真正提出警報:

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Log Out", style: .destructive, handler: { (_) in

ProfileServices.shared.signOutUser()

let signInVC = SigninViewController()
let navController = UINavigationController(rootViewController: signInVC)
self.present(navController, animated: true)
}))
You need to present the alert here, by doing something like:
self.present(alert, animated: true)
self.navigationController?.present(alert, animated: true, completion: nil) 

您已經創建了 UIAlertController 的一個實例,但沒有在任何地方展示它。 你應該已經提出了。 嘗試,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            let selectSchoolVC = SelectSchoolPopUpVC()
            selectSchoolVC.modalPresentationStyle = .fullScreen
            selectSchoolVC.modalTransitionStyle   = .crossDissolve
            present(selectSchoolVC, animated: true, completion: nil)
        } else if indexPath.section == 1 {

        } else if indexPath.section == 2 {

        } else if indexPath.section == 3 {

        }

        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Log Out", style: .destructive, handler: { (_) in

            ProfileServices.shared.signOutUser()

            let signInVC = SigninViewController()
            let navController = UINavigationController(rootViewController: signInVC)
            self.present(navController, animated: true)
        }))

        self.present(alert, animated: true)

        tableView.deselectRow(at: indexPath, animated: true)
    }

您需要在最后的didSelect中展示您的警報控制器

self.present(alert, animated: true)
tableView.deselectRow(at: indexPath, animated: true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM