gpt4 book ai didi

swift - 如何在选择一行后隐藏tableView

转载 作者:行者123 更新时间:2023-11-30 10:49:35 26 4
gpt4 key购买 nike

我有一个文本字段,触摸时会显示一个包含一些行的 TableView 。

我正在尝试执行此操作:当用户选择其中一行时,行的值将放置在文本字段中,并且 tableView 会关闭。

第一部分对我来说效果很好。用户触摸一行,文本字段会显示该行的值。但如果我想关闭表格 View ,我必须在该行上按两次。

这是我的代码:

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
}

}

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

有什么建议吗?

最佳答案

详细信息

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

关于swift - 如何在选择一行后隐藏tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54923590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com