UISearchbar 在 swift 4 中使用 UItextfield

2024-04-18

我正在研究 JSON。我的 Json 数据打印到表视图中。我想用搜索栏过滤该数据。所以我放置了 Textfield 以使用搜索栏。我使用这个网站的参考

http://findnerd.com/list/view/How-to-create-your-own-search-bar-in-Swift-Not-using-UISearchBar/20577/ http://findnerd.com/list/view/How-to-create-your-own-search-bar-in-Swift-Not-using-UISearchBar/20577/

我的搜索栏可以工作,但不正常。我想在搜索栏中输入 3 个单词后过滤数据。如果我输入“Ku”,那么我的表格视图将保持隐藏状态。如果我在搜索栏中写“kus”,那么搜索栏就会开始搜索并在表格视图中显示从“kus”开始的过滤数据。我的搜索栏相关代码是这些

struct PatientData:Decodable {
var ID : String
var dt_bod : String
var e_gender : String
var int_glcode : String
var var_email : String
var var_fname : String
var var_phoneno : String
var var_uname : String

init(userdata : [String:Any]) {
    self.ID = userdata["ID"] as! String
    self.dt_bod = userdata["dt_bod"] as! String
    self.e_gender = userdata["e_gender"] as! String
    self.int_glcode = userdata["int_glcode"] as! String
    self.var_email = userdata["var_email"] as! String
    self.var_fname = userdata["var_fname"] as! String
    self.var_phoneno = userdata["var_phoneno"] as! String
    self.var_uname = userdata["var_uname"] as! String

} 

var tabledata = [String]()
var tableFilterData = [String]()
var patientDetails = [PatientData]()

@IBAction func textfieldchanged(_ sender: Any) {
    tableview.isHidden = true
}

我的文本字段更改字符功能

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{

    let searchText  = textField.text! + string
    if searchText.count >= 3 {
        tableview.isHidden = false

        tableFilterData = tabledata.filter({ (result) -> Bool in
            return result.range(of: searchText, options: .caseInsensitive) != nil
        })

        print(tableFilterData)   // I got filtered data here but how to show this data into the tableview
        tableview.reloadData()
    }
    else{
        tableFilterData = []
    }
    return true
}

表格视图部分是

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

        return patientDetails.count
     }

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

    let aa = patientDetails[indexPath.row].var_fname + " , " + patientDetails[indexPath.row].dt_bod + " , " + patientDetails[indexPath.row].var_phoneno

    self.tabledata.append(aa)

        cell.textLabel?.text = aa
        cell.textLabel?.font = searchTextfield.font

    return cell
}

尝试这个:

@objc func textFieldActive() {
    tableView.isHidden = tableFilterData.count > 0 ? false : true
}

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{

    let searchText  = textField.text! + string

    if searchText.count >= 3 {
        tableView.isHidden = false

        tableFilterData = tabledata.filter({ (result) -> Bool in
            return result.range(of: searchText, options: .caseInsensitive) != nil
        })

        tableView.reloadData()
    }
    else{
        tableFilterData = []
    }

    return true
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let data = tableFilterData[indexPath.row]

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

    cell.textLabel?.text = data

    return cell
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UISearchbar 在 swift 4 中使用 UItextfield 的相关文章

随机推荐

  • c - 将 uint8_t* 转换为 uint32_t* 行为

    我读过这个问题 将 uint8 转换为 uint32 如何工作 https stackoverflow com questions 28603243 how does casting uint8 to uint32 work但我不确定给出的
  • 标准库中聚合可初始化性的类型特征?

    C 标准库有std is constructible
  • 在 Eclipse 之外使用 Maven

    我已经在 Eclipse 之外使用 Maven 但想使用 Eclipse 作为我的主编辑器 我不想使用已经嵌入 M2E 的 Maven 我想使用位于 usr bin mvn 的我的 在 Window gt Preferences 下 我尝试
  • 从 JDBC 结果集中获取 XML 的最佳方法

    我正在寻找从 JDBC 结果集中获取 XML 文档的最佳方法 XML 的结构并不是非常重要 但它应该相当快 为了清楚起见 我想要结果集中的数据和足够的元数据来识别数据 本质上是字段名称 我目前正在使用 MySQL DB2 SQL Serve
  • 礼品卡/借记卡激活

    一般问题 零售机构如何限制礼品卡或预付费电话 借记卡的激活 他们必须有一个系统 只能阻止您打电话激活未通过收银机扫描的卡 而且我认为零售 ERP 会计系统中必须内置一个标准解决方案 它可能涉及 Web 服务或 EDI 具体问题 我问这一切是
  • “破坏对象”的variable = null从何而来?

    在许多不同的公司中 我在使用各种 NET 版本编写的许多遗留系统上工作 不断发现以下模式的示例 public void FooBar object foo null object bar null try foo new object ba
  • “用户模板”未显示在“新项目”窗口中 - Xcode 4

    我最近从 SDL 网站下载了两个 SDL 包并将它们移动到相应的文件夹中 在 Library Application Support Developer Shared Xcode Project Templates 中 我创建了一个名为 A
  • 如何在 Python 中使数据正确符合幂律?

    我正在考虑Moby Dick 小说中独特单词的出现次数 http tuvalu santafe edu 7Eaaronc powerlaws data htm并使用powerlaw蟒蛇包 https pythonhosted org pow
  • 在 Interface Builder 中的 View Effects Inspector 中选择 CALayer 有何作用?

    在IB内部 在View Effects Inspector中 有一个设置核心动画层的选项 如果我设置了它会做什么 是否相当于setWantsLayer YES 如果是这样 那么按照苹果文档 https developer apple com
  • Flink 使用 Ceph 作为持久存储

    Flink 文档建议 Ceph 可以用作状态的持久存储 https ci apache org projects flink flink docs release 1 3 dev stream checkpointing html http
  • Django 中多个模型的单个自定义管理器

    我有几个模型通过外键关系相互连接 这种层次结构中的主要层次包含一个owner field 我想创建一个单一定制经理 for 所有这些模型根据调用它的模型更改返回的查询集 我知道经理可以访问self model获取它所附加的模型 Class
  • 编程测试 - Codility - Dominator [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我只是遇到了一个 codility 问题让我很困难 我仍在尝试找出如何满足空间和时间复杂性限制 问题如下 数组中的主导成员是指占据数组
  • 在 Protege 中使用 SPARQL 推断数据

    我正在尝试推断 RDF 数据 假设我有这些三元组 RDF Turtle 是我使用 Protege 创建的 prefix owl
  • 用于 Angular E2E 测试的 Protractor:管理测试数据的最佳方法是什么?

    我是新来的Angular and Protractor 我编写了几个量角器测试用例来自动注册 登录和其他一些页面 要执行测试用例 例如学生注册 我需要传递一些数据 例如姓名 出生日期 年龄 地址等 现在我在测试用例中对这些值进行硬编码 这不
  • 将开发分支与主分支合并

    我有两个分支master and development在 GitHub 存储库中 我正在开发分支中进行所有开发 如图所示 git branch development git add git commit m My initial com
  • 通过 .net 客户端通过 SSL 连接到 IBM MQ

    我正在尝试通过 NET 客户端连接到 MQ 服务器队列 我需要使用证书进行安全通信 这是我的代码 MQEnvironment SSLKeyRepository SYSTEM MQEnvironment ConnectionName conn
  • 在组上方展开 Android 的 ExpandableListView 子级

    ExpandableListView 实现的默认行为是在单击时展开组下方的子项 我想知道是否可以通过修改 ExpandableListView XML 属性或以某种方式以编程方式更改此行为 以便在组上方生成子级 单击时 我最终通过制作一个普
  • PyMol:选择我发现氢键的残基

    使用 PyMol 我可以使用 Action gt Find gt Polar Contacts 显示氢键 这会产生接触点 但我想通过仅显示具有这些接触点的残基而不显示其他内容来清楚地显示它们 目前 由于蛋白质的其余部分 视图非常不清楚 我想
  • Unordered_map 迭代器失效

    我有这个迭代器循环 typedef boost unordered map
  • UISearchbar 在 swift 4 中使用 UItextfield

    我正在研究 JSON 我的 Json 数据打印到表视图中 我想用搜索栏过滤该数据 所以我放置了 Textfield 以使用搜索栏 我使用这个网站的参考 http findnerd com list view How to create yo