TVOS:使用UITableView实现下拉列表的行为很奇怪

问题描述

我遵循这个tutorial来实现一个简单的下拉列表。这是代码

import UIKit

class CellClass: UITableViewCell {
    
}

class TestDropDownViewController: UIViewController {

    @IBOutlet weak var btnSelectFruit: UIButton!
    @IBOutlet weak var btnSelectGender: UIButton!
    
    let transparentView = UIView()
    let tableView = UITableView()
    
    var selectedButton = UIButton()
    
    var dataSource = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(CellClass.self,forCellReuseIdentifier: "Cell")
    }
    
    func addTransparentView(frames: CGRect) {
        let window = UIApplication.shared.keyWindow
        transparentView.frame = window?.frame ?? self.view.frame
        self.view.addSubview(transparentView)
        
        tableView.frame = CGRect(x: frames.origin.x,y: frames.origin.y + frames.height,width: frames.width,height: 0)
        self.view.addSubview(tableView)
        tableView.layer.cornerRadius = 5
        
        transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.9)
        tableView.reloadData()
        let tapgesture = UITapGestureRecognizer(target: self,action: #selector(removeTransparentView))
        transparentView.addGestureRecognizer(tapgesture)
        transparentView.alpha = 0
        UIView.animate(withDuration: 0.4,delay: 0.0,usingSpringWithdamping: 1.0,initialSpringVeLocity: 1.0,options: .curveEaseInOut,animations: {
            self.transparentView.alpha = 0.5
            self.tableView.frame = CGRect(x: frames.origin.x,y: frames.origin.y + frames.height + 5,height: CGFloat(self.dataSource.count * 50))
        },completion: nil)
    }
    
    @objc func removeTransparentView() {
        let frames = selectedButton.frame
        UIView.animate(withDuration: 0.4,animations: {
            self.transparentView.alpha = 0
            self.tableView.frame = CGRect(x: frames.origin.x,height: 0)
        },completion: nil)
    }

    @IBAction func onClickSelectFruit(_ sender: Any) {
        dataSource = ["Apple","Mango","Orange"]
        selectedButton = btnSelectFruit
        addTransparentView(frames: btnSelectFruit.frame)
    }
    
    @IBAction func onClickSelectGender(_ sender: Any) {
        dataSource = ["Male","Female"]
        selectedButton = btnSelectGender
        addTransparentView(frames: btnSelectGender.frame)
    }
}

extension TestDropDownViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath)
        cell.textLabel?.text = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        selectedButton.setTitle(dataSource[indexPath.row],for: .normal)
        removeTransparentView()
    }
}

我在iPhone上对其进行了测试,并且效果很好:
我点击下拉按钮。
列表打开。
我选择一个值。
列表关闭
它成为下拉按钮中的显示值。

但是,在TVOS中,完全相同的代码表现得很奇怪。
STRANGE BEHAVIOR 1::从下拉列表中选择一个值后,它保持打开状态:

enter image description here


实际上,我可以继续滚动这些值并选择一个新值!

所以我认为,如果我找到关闭下拉列表的方法,那么我的问题将得到解决
STRANGE BEHAVIOR 2::选择一个值然后重新打开下拉列表后,如果尝试向下滚动第一个值,它将自动再次突出显示一个值。即:值1突出显示,向下滚动,值2快速突出显示,值1再次突出显示。唯一能够正常滚动的方法是,当值1突出显示时,我必须再次单击“向上”。

enter image description here


所以我被困在那里,直到由于某种原因再次单击“向上” ...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)