gpt4 book ai didi

ios - 是什么使搜索栏选项所选择的Scope ButtonIndexDidChange执行?

转载 作者:行者123 更新时间:2023-12-01 17:39:08 25 4
gpt4 key购买 nike

我正在尝试快速学习新的UISearchController。那里还没有太多例子。
我一直在寻找添加范围选项,以便当用户在搜索中选择范围时,列表将被范围过滤。我一直在看编程的iOS 8 O'Reilly书籍,示例。他们有一个示例,其中过滤在一个屏幕中完成。
我意识到实际的搜索将不适用于我拥有的当前范围值。目前我所要做的就是使该功能正常运行。我正在使用此代码测试想法,然后将这些想法移植到我的应用程序中。
如果我输入文本搜索,那么所有功能都可以使用,但是当我尝试使用示波器时,它没有用。这是代码。关于为什么永不调用以下代码的任何提示:

func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
println("In selectedScopeButtonIndexDidChange")
updateSearchResultsForSearchController(searcher)
}

这是完整的代码。
import UIKit

class ViewController: UITableViewController, UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating {


var sectionNames = [String]()
var sectionData = [[String]]()
var originalSectionNames = [String]()
var originalSectionData = [[String]]()
var searcher = UISearchController()
var searching = false

override func prefersStatusBarHidden() -> Bool {
return true
}

override func viewDidLoad() {
//super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let s = NSString(contentsOfFile: NSBundle.mainBundle().pathForResource("states", ofType: "txt")!, encoding: NSUTF8StringEncoding, error: nil)!
let states = s.componentsSeparatedByString("\n") as [String]
var previous = ""
for aState in states {
// get the first letter
let c = (aState as NSString).substringWithRange(NSMakeRange(0,1))
// only add a letter to sectionNames when it's a different letter
if c != previous {
previous = c
self.sectionNames.append( c.uppercaseString )
// and in that case also add new subarray to our array of subarrays
self.sectionData.append( [String]() )
}
sectionData[sectionData.count-1].append( aState )
}
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "Header")

self.tableView.sectionIndexColor = UIColor.whiteColor()
self.tableView.sectionIndexBackgroundColor = UIColor.redColor()
// self.tableView.sectionIndexTrackingBackgroundColor = UIColor.blueColor()
// self.tableView.backgroundColor = UIColor.yellowColor()
self.tableView.backgroundView = { // this will fix it
let v = UIView()
v.backgroundColor = UIColor.yellowColor()
return v
}()

// in this version, we take the total opposite approach:
// we don't present any extra view at all!
// we already have a table, so why not just filter the very same table?
// to do so, pass nil as the search results controller,
// and tell the search controller not to insert a dimming view

// keep copies of the original data
self.originalSectionData = self.sectionData
self.originalSectionNames = self.sectionNames
let searcher = UISearchController(searchResultsController:nil)
self.searcher = searcher
searcher.dimsBackgroundDuringPresentation = false
searcher.searchResultsUpdater = self
searcher.delegate = self
// put the search controller's search bar into the interface
let b = searcher.searchBar
b.sizeToFit() // crucial, trust me on this one
b.autocapitalizationType = .None
b.scopeButtonTitles = ["All", "S", "X"] // won't show in the table
**b.delegate = self**
self.tableView.tableHeaderView = b
self.tableView.reloadData()
//self.tableView.scrollToRowAtIndexPath(
// NSIndexPath(forRow: 0, inSection: 0),
// atScrollPosition:.Top, animated:false)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sectionNames.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.sectionData[section].count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let s = self.sectionData[indexPath.section][indexPath.row]
cell.textLabel!.text = s

// this part is not in the book, it's just for fun
var stateName = s
stateName = stateName.lowercaseString
stateName = stateName.stringByReplacingOccurrencesOfString(" ", withString:"")
stateName = "flag_\(stateName).gif"
let im = UIImage(named: stateName)
cell.imageView!.image = im

return cell
}

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let h = tableView.dequeueReusableHeaderFooterViewWithIdentifier("Header") as UITableViewHeaderFooterView
if h.tintColor != UIColor.redColor() {
h.tintColor = UIColor.redColor() // invisible marker, tee-hee
h.backgroundView = UIView()
h.backgroundView!.backgroundColor = UIColor.blackColor()
let lab = UILabel()
lab.tag = 1
lab.font = UIFont(name:"Georgia-Bold", size:22)
lab.textColor = UIColor.greenColor()
lab.backgroundColor = UIColor.clearColor()
h.contentView.addSubview(lab)
let v = UIImageView()
v.tag = 2
v.backgroundColor = UIColor.blackColor()
v.image = UIImage(named:"us_flag_small.gif")
h.contentView.addSubview(v)
lab.setTranslatesAutoresizingMaskIntoConstraints(false)
v.setTranslatesAutoresizingMaskIntoConstraints(false)
h.contentView.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:|-5-[lab(25)]-10-[v(40)]",
options:nil, metrics:nil, views:["v":v, "lab":lab]))
h.contentView.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:|[v]|",
options:nil, metrics:nil, views:["v":v]))
h.contentView.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:|[lab]|",
options:nil, metrics:nil, views:["lab":lab]))
}
let lab = h.contentView.viewWithTag(1) as UILabel
lab.text = self.sectionNames[section]
return h
}

// much nicer without section index during search
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
return self.searching ? nil : self.sectionNames
}

func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
println("In selectedScopeButtonIndexDidChange")
updateSearchResultsForSearchController(searcher)
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
let sb = searchController.searchBar
let target = sb.text
println("target is: \(target)")
if target == "" {
self.sectionNames = self.originalSectionNames
self.sectionData = self.originalSectionData
self.tableView.reloadData()
return
}
// we have a target string
self.sectionData = self.originalSectionData.map {
$0.filter {
let options = NSStringCompareOptions.CaseInsensitiveSearch
let found = $0.rangeOfString(target, options: options)
return (found != nil)
}
}.filter {$0.count > 0} // is Swift cool or what?
self.sectionNames = self.sectionData.map {prefix($0[0],1)}
self.tableView.reloadData()
}

}

最佳答案

这个问题的答案是添加:

b.delegate = self

进入viewDidLoad()部分。

我已经添加了新行,更新了上面的代码。

关于ios - 是什么使搜索栏选项所选择的Scope ButtonIndexDidChange执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27662231/

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