选择行后如何隐藏tableView

时间:2019-02-28 10:35:34

标签: swift uitableview swift3 autocomplete

我有一个textField,当触摸时它会显示带有一些行的tableView。

我正在尝试这样做:当用户选择行之一时,将row的值放置在textField中,并关闭tableView。

第一部分对我来说效果很好。用户触摸一行,textField显示该行的值。但是,如果要关闭表格视图,则必须在该行上按两次。

这是我的代码:

class Redactar_mensaje: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {


    var values = ["123 Main Street", "789 King Street", "456 Queen Street", "99 Apple Street", "red", "orange", "yellow", "green", "blue", "purple", "owaldo", "ostras", "Apple", "Pineapple", "Orange", "Adidas"]

@IBOutlet weak var campo_para: UITextField!
@IBOutlet weak var tableView: UITableView!

    var originalCountriesList:[String] = Array()

override func viewDidLoad() {
        super.viewDidLoad()

        tableView.isHidden = true


        for country in values {
           originalCountriesList.append(country)
        }

        campo_para.delegate = self
        tableView.delegate = self
        tableView.dataSource = self

        campo_para.addTarget(self, action: #selector(textFieldActive), for: UIControlEvents.touchDown)
        campo_para.addTarget(self, action: #selector(searchRecords(_ :)), for: .editingChanged)
    }


    @objc func searchRecords(_ textField: UITextField) {
        self.values.removeAll()
        if textField.text?.count != 0 {
            for country in originalCountriesList {
                if let countryToSearch = textField.text{
                    let range = country.lowercased().range(of: countryToSearch, options: .caseInsensitive, range: nil, locale: nil)
                    if range != nil {
                        self.values.append(country)
                    }
                }
            }
        } else {
            for country in originalCountriesList {
                values.append(country)
            }
        }

        tableView.reloadData()
    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return values.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellx")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cellx")
        }
        cell?.textLabel?.text = values[indexPath.row]
        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        campo_para.text = values[indexPath.row]

        tableView.isHidden = true //I need press twice for this. I want press only one

    }


    func textFieldActive() {
        tableView.isHidden = false
    }

}

理想情况下,用户触摸textField,显示tableView,选择其中一个值,然后自动关闭tableView。但是最后一个效果不佳。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

详细信息

xCode 8.3,Swift 3.1

在TableViewCell上检测双击和单击的示例

  

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
    }
}

extension ViewController: UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
        cell.label.text = "\(indexPath)"
        cell.delegate = self
        return cell
    }
}

extension ViewController:TableViewCellDelegate {


    func tableViewCell(singleTapActionDelegatedFrom cell: TableViewCell) {
        let indexPath = tableView.indexPath(for: cell)
        print("singleTap \(String(describing: indexPath)) ")
    }

    func tableViewCell(doubleTapActionDelegatedFrom cell: TableViewCell) {
        let indexPath = tableView.indexPath(for: cell)
        print("doubleTap \(String(describing: indexPath)) ")
        //You can hide your textfield here
    }
}
  

TableViewCell.swift

import UIKit

class TableViewCell: UITableViewCell {

    @IBOutlet weak var label: UILabel!
    private var tapCounter = 0
    var delegate: TableViewCellDelegate?


    override func awakeFromNib() {
        super.awakeFromNib()

        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        addGestureRecognizer(tap)
    }

    func tapAction() {

        if tapCounter == 0 {
            DispatchQueue.global(qos: .background).async {
                usleep(250000)
                if self.tapCounter > 1 {
                    self.doubleTapAction()
                } else {
                    self.singleTapAction()
                }
                self.tapCounter = 0
            }
        }
        tapCounter += 1
    }

    func singleTapAction() {
        delegate?.tableViewCell(singleTapActionDelegatedFrom: self)
    }

    func doubleTapAction() {
        delegate?.tableViewCell(doubleTapActionDelegatedFrom: self)
    }
}
  

TableViewCellDelegate.swift

import UIKit

protocol TableViewCellDelegate {
    func tableViewCell(singleTapActionDelegatedFrom cell: TableViewCell)
    func tableViewCell(doubleTapActionDelegatedFrom cell: TableViewCell)
}

结果

enter image description here

答案 1 :(得分:0)

在这里我提出了解决方案,以防其他人发生类似的事情。

只需更改行的顺序,然后再添加一行即可。首先,它使其不可见,然后将结果放入textField中,神奇地,它起作用了!

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

        tableView.isHidden = true
        campo_para.text = NombreUsuario[indexPath.row]
        campo_asunto.becomeFirstResponder()

    }

谢谢!

相关问题