Swift-UITextField搜索以将行同时添加到UITableView

时间:2018-08-14 14:55:17

标签: swift uitableview search uitextfield

我具有此搜索功能:

func searchFor(_ text: String) {
    for path in allPoemsPaths {
        for poem in Helper.getPoems(filePath: path) {
            if poem.body.applyingTransform(.stripDiacritics, reverse: false)!.contains(text) {
                searchResults.append(poem)

                resultsTable.beginUpdates()
                resultsTable.insertRows(at: [IndexPath(row: searchResults.count-1, section: 0)], with: .automatic)
                resultsTable.endUpdates()
            }
        }
    }
}

它由UITextField委托调用:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    searchTextField.resignFirstResponder()

    searchResults.removeAll()
    resultsTable.reloadData()

    searchFor(" " + textField.text! + " ")

    return true
}

此搜索是通过十万个文本文件进行的。

我想在找到搜索到的文本后动态/同时添加一行,并且即使搜索未完成也能够选择它。

我得到的是搜索完成后添加了行。

有什么想法吗?

谢谢。

更新:

getPoems函数:

static func getPoems(filePath: String) -> [Poem] {

    // Extracting author name
    let array = filePath.components(separatedBy: "/").last!.components(separatedBy: "_")
    let arabicName = array[2]

    var poems = [Poem]()
    let poemsString = try! String(contentsOfFile: filePath, encoding: .utf8)

    // separate the file by '---' into array
    var poemsArray = poemsString.components(separatedBy: "---")

    // First item in the array is empty
    poemsArray.removeFirst()

    for poemItem in poemsArray {
        var poem = Poem()

        if let poemBody = getXmlItem("poem", from: poemItem) {
            poem.body = poemBody
        } else {
            continue
        }

        // Extract title
        if let indexOfSlash = poem.body.index(of: "/") {
            poem.title = String(poem.body[..<indexOfSlash])
        } else {
            continue
        }

        if let i = getXmlItem("index", from: poemItem) {
            poem.index = Int(i) ?? 0
        }
        if let n = getXmlItem("versesNumber", from: poemItem) {
            poem.numberOfVerses = Int(n) ?? 0
        }
        poem.bahr = getXmlItem("bahr", from: poemItem) ?? " "

        poem.qafiya = getXmlItem("qafiya", from: poemItem) ?? " "

        poem.author = arabicName

        poems.append(poem)
    }

    return poems
}

1 个答案:

答案 0 :(得分:1)

尝试像这样更改您的方法

func searchFor(_ text: String) {
    DispatchQueue.global().async {
        for path in self.allPoemsPaths {
            for poem in Helper.getPoems(filePath: path) {
                if poem.body.applyingTransform(.stripDiacritics, reverse: false)!.contains(text) {

                    DispatchQueue.main.async {
                        self.searchResults.append(poem)
                        self.resultsTable.beginUpdates()
                        self.resultsTable.insertRows(at: [IndexPath(row: self.searchResults.count-1, section: 0)], with: .automatic)
                        self.resultsTable.endUpdates()
                    }
                }
            }
        }
    }
}