SearchTextField.swift中遇到错误

时间:2018-07-09 03:48:26

标签: ios swift

在调试下面的代码时,我需要一些帮助。非常感谢您的帮助,非常感谢

Image Error

    fileprivate func filter(forceShowAll addAll: Bool) {
        clearResults()

        if text!.count < minCharactersNumberToStartFiltering {
            return
        }

        for i in 0 ..< filterDataSource.count {

            let item = filterDataSource[i]

            if !inlineMode {
                // Find text in title and subtitle
                let titleFilterRange = (item.title as NSString).range(of: text!, options: comparisonOptions)
                let subtitleFilterRange = item.subtitle != nil ? (item.subtitle! as NSString).range(of: text!, options: comparisonOptions) : NSMakeRange(NSNotFound, 0)

                if titleFilterRange.location != NSNotFound || subtitleFilterRange.location != NSNotFound || addAll {
                    item.attributedTitle = NSMutableAttributedString(string: item.title)
                    item.attributedSubtitle = NSMutableAttributedString(string: (item.subtitle != nil ? item.subtitle! : ""))

                    item.attributedTitle!.setAttributes(Any,], range: titleFilterRange)

                    if subtitleFilterRange.location != NSNotFound {
                        item.attributedSubtitle!.setAttributes(NSAttributedString as [String : Any], range: subtitleFilterRange)
                    }

                    filteredResults.append(item)
                }
            } else {
                var textToFilter = text!.lowercased()

                if inlineMode, let filterAfter = startFilteringAfter {
                    if let suffixToFilter = textToFilter.components(separatedBy: filterAfter).last, (suffixToFilter != "" || startSuggestingInmediately == true), textToFilter != suffixToFilter {
                        textToFilter = suffixToFilter
                    } else {
                        placeholderLabel?.text = ""
                        return
                    }
                }

                if item.title.lowercased().hasPrefix(textToFilter) {
                    let indexFrom = textToFilter.index(textToFilter.startIndex, offsetBy: textToFilter.count)
                    let itemSuffix = item.title[indexFrom...]

                    item.attributedTitle = NSMutableAttributedString(string: String(itemSuffix))
                    filteredResults.append(item)
                }
            }
        }

        tableView?.reloadData()

        if inlineMode {
            handleInlineFiltering()
        }
    }

    // Clean filtered results
    fileprivate func clearResults() {
        filteredResults.removeAll()
        tableView?.removeFromSuperview()
    }

1 个答案:

答案 0 :(得分:0)

您需要提供attributes,因为您可以看到此类here的实际实现。您可以按照以下步骤进行操作,

item.attributedTitle!.setAttributes(highlightAttributes, range: titleFilterRange)

if subtitleFilterRange.location != NSNotFound {                          
  item.attributedSubtitle!.setAttributes(highlightAttributesForSubtitle(), range: 
  subtitleFilterRange)
}