簡體   English   中英

DidSelectRow 不起作用

[英]DidSelectRow is not working

幾乎是標題所說的: DidSelectRow沒有被調用,我確實檢查過我沒有使用Deselect 我還檢查了delegateDataSource是否連接到tableViewController並且SelectionSingle Selection 但是,該方法仍未被調用。

一個空的tableView是否可能對它有任何影響(它為空的原因是因為用戶開始填充它。我正在使用CoreData )?

編輯

這是該方法的樣子:

 var effectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    var effect: UIVisualEffect!
    let defaults = UserDefaults.standard
    var userMagazineTitle = [User]()
    var dataString = String()

override func viewDidLoad() {
        super.viewDidLoad()
        addProgrammatically()

        let fetchRequest: NSFetchRequest<User> = User.fetchRequest()
        do{
            let users = try PresistanceService.context.fetch(fetchRequest)
            self.userMagazineTitle = users
            self.tableView.reloadData()
        }catch{
            ProgressHUD.showError("Nothing to see here")
        }

    } 

 / MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return userMagazineTitle.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyFeedTableViewCell
        cell.myHeadline?.text = userMagazineTitle[indexPath.row].title
        cell.indentationLevel = 3
        return cell
    }

    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        let manage = PresistanceService.persistentContainer.viewContext
        let del = userMagazineTitle[indexPath.row]

        if editingStyle == .delete {
            // Delete the row from the data source
            manage.delete(del)
            do{
                try manage.save()
            }catch{
                ProgressHUD.showError()
            }

            let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")

            do{
                userMagazineTitle = try manage.fetch(fetchRequest) as! [User]
            }catch{
                ProgressHUD.showError()
            }
            tableView.reloadData()
        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "showUser", sender: indexPath)
        navigationController?.navigationBar.isHidden = false
        tableView.deselectRow(at: indexPath, animated: true)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showUser" {
            let DestViewController = segue.destination as! UINavigationController
            let targetController = DestViewController.topViewController as! CompanyTableViewController
            let indexPath = sender as! IndexPath
            let user = userMagazineTitle[indexPath.row].title
            let user2 = userMagazineTitle[indexPath.row].rssurl
            targetController.customUserInit(articlesindex: indexPath.row, title: user!, rssUrl: user2!)
        }

    }

    func addProgrammatically() {
        effectView.frame = view.bounds
        tableView.addSubview(effectView)

        effectView.translatesAutoresizingMaskIntoConstraints = false
        if #available(iOS 11.0, *) {
            effectView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        } else {
            // Fallback on earlier versions
            effectView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
        }
        effectView.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true
        effectView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true

        effect = effectView.effect
        effectView.effect = nil

        self.tableView.separatorStyle = .none
        tableView.backgroundView = UIImageView(image: UIImage(named: "myMagazines.jpg"))
        tableView.backgroundView?.contentMode = .scaleAspectFill
        tableView.clipsToBounds = true
        navigationController?.navigationBar.isHidden = false
        UIApplication.shared.statusBarStyle = .lightContent
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.barStyle = .black
        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    }

}

可能導致問題的另一件事是未選擇選擇種類。 我花了兩個小時來解決這個問題。

在此處輸入圖片說明

交叉檢查單元格選擇,如果它被選中沒有選擇將其更改為單選。 要以編程方式執行此操作:

斯威夫特: tableView.allowsSelection = YES;

目標 C: [tableView allowsSelection : YES];

==============================

這可能對某人有幫助!

您在 tableview 之上添加了一個UIVisualEffectView視圖,它沒有將觸摸事件傳遞給 tableview。

設置effectView.isUserInteractionEnabled = false ,應該使這個視圖將觸摸事件傳遞給下面的視圖。

這個問題可能有兩個問題:

  1. TableView DidSelectRow 的方法簽名可能是錯誤的。 目前的簽名是:

    Swift: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

    ObjectiveC: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

  2. 您正在代碼中的某處將 TableView 的委托設置為 nil。

暫無
暫無

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

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