【问题标题】:Search bar in UITableView for Realm objects领域对象的 UITableView 中的搜索栏
【发布时间】:2015-12-31 17:10:17
【问题描述】:

我有一个由领域对象填充的表格视图,当我单击一个单元格时,它会将我带到另一个视图,该视图向我显示有关该对象的更多信息。我想为这个表视图添加一个搜索栏来检查对象的“fullName”属性。我知道如何为字符串数组添加搜索栏,但我还没有找到任何关于如何使用 Swift 2.0 的教程或指导。有什么帮助吗?

编辑:这就是我填充表格视图的方式

var dataSource : Results<Patient>!

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let identifier: String = "myCell"
    var cell = tableView.dequeueReusableCellWithIdentifier(identifier)

    if cell == nil {
        cell = UITableViewCell(style: .Subtitle, reuseIdentifier: identifier)
    }

    let currentPatientInfo = dataSource[indexPath.section]

    cell?.textLabel?.text = currentPatientInfo
    cell?.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton

    return cell!

}

我有部分数据,而不是行

编辑:我将RealmSwiftSearchController 添加到我的项目中,但有时在单击单元格时会出现此错误:

致命错误:在展开可选值时意外发现 nil

它指向这段代码:

extension RealmSearchViewController {


 public override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

// The line below
    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Object
// Line ends
        self.resultsDelegate.searchViewController(self, willSelectObject: object, atIndexPath: indexPath)

        return indexPath
    }

【问题讨论】:

    标签: ios swift uitableview uisearchbar realm


    【解决方案1】:

    有一个名为RealmSearchViewController 的开源组件应该能够处理设置可搜索UITableView 以及与Realm 互操作所需的几乎所有样板代码:https://github.com/bigfish24/ABFRealmSearchViewController

    此外,Realm 网站上有一个关于如何实现它的教程:https://realm.io/news/building-an-ios-search-controller-in-swift/

    【讨论】:

    • 我试过了,当我用对象填充我的表格视图时,只有第一行显示病人的名字,我可以点击它,但其他行是空白的,只要我点击它们,它通过告诉我 > 致命错误而崩溃:在展开可选值时意外发现 nil ... 它指向 ...
    • extension RealmSearchViewController { public override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -&gt; NSIndexPath? { /*!*/ let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Object /*!*/ self.resultsDelegate.searchViewController(self, willSelectObject: object, atIndexPath: indexPath) return indexPath }
    • 好的,所以我在部分和行之间切换,数据出现了。但是,无论我搜索什么,我要么只得到第一行,要么得到空白(可点击并给我同样的错误)行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多