gpt4 book ai didi

ios - UISearchBar 未更新

转载 作者:行者123 更新时间:2023-11-30 13:53:06 24 4
gpt4 key购买 nike

我有一个 UIViewController,我在其中添加了 UITableViewController,并且我以编程方式添加搜索栏。我可以在控制台上搜索后打印结果,但是当我单击搜索栏时,自定义表格单元格仍然会填充,并且搜索根本不会更新它们。

我在 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 中有打印语句来检查搜索 Controller 是否处于事件状态,并且它从不打印该语句。

import UIKit
import Parse

class MasterViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {

@IBOutlet var mTableView: UITableView!
var detailViewController: DetailViewController? = nil
var objects = [AnyObject]()
var resultSearchController = UISearchController()
var filteredData : NSMutableArray = []
var sectionInTable : NSMutableArray = ["Grand Haven 9", "Holland 7"]
var movieTitle : NSMutableArray = ["The Hunger Game 2","Creed"]
var movieTimeSection1 : NSMutableArray = ["11:00am, 11:40, 12:10pm, 1:25, 2:30, 6:05, 6:55, 7:30, 9:05", "11:10am, 11:50, 12:20pm, 2:25, 3:30, 6:05, 6:55, 8:30, 10:05"]
var movieTimeSection2 = ["Title": "Martian", "Theatre": "Holland 7", "Time": "11:00am, 11:40, 12:10pm, 1:25, 2:30, 6:05, 6:55, 7:30, 9:05"]


var onlineMovieTitle : NSMutableArray = []

var movieSection1 : NSMutableArray = []
var movieSection2 : NSMutableArray = []

override func viewDidLoad() {
super.viewDidLoad()

print(movieSection1)
print(movieSection2)

self.tableView.separatorColor = UIColor.cloudsColor()
self.tableView.backgroundColor = UIColor.cloudsColor()

self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.delegate = self
controller.searchBar.sizeToFit()
controller.preferredStatusBarStyle() == UIStatusBarStyle.LightContent
controller.hidesNavigationBarDuringPresentation = false
self.mTableView.tableHeaderView = controller.searchBar

return controller
})()
//self.resultSearchController.searchBar.endEditing(true)

}
func updateSearchResultsForSearchController(searchController: UISearchController){
filteredData.removeAllObjects()

let searchPredicate = NSPredicate(format: "Title CONTAINS %@", searchController.searchBar.text!.uppercaseString)
let array = (movieSection1 as NSArray).filteredArrayUsingPredicate(searchPredicate)
let array2 = (movieSection2 as NSArray).filteredArrayUsingPredicate(searchPredicate)
//let array2 = (movieTimeSection1 as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredData.addObjectsFromArray(array)
filteredData.addObjectsFromArray(array2)

print(filteredData)
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.resultSearchController.searchBar.hidden = false
}

// MARK: - Segues

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {

}

}
}

// MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count : Int = 0
if (self.resultSearchController.active) {
return 2
}
else {
if section == 0 {
count = movieSection1.count
} else if section == 1 {
count = movieSection2.count
}
}
return count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : MainTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MainTableViewCell

//Fancy Cells
var corners : UIRectCorner = UIRectCorner.AllCorners
if (tableView.numberOfRowsInSection(indexPath.section) == 1) {
corners = UIRectCorner.AllCorners
} else if (indexPath.row == 0) {
corners = UIRectCorner.TopLeft.union(UIRectCorner.TopRight)
} else if (indexPath.row == tableView.numberOfRowsInSection(indexPath.section) - 1) {
corners = UIRectCorner.BottomLeft.union(UIRectCorner.BottomRight)
}
cell.configureFlatCellWithColor(UIColor.greenSeaColor(), selectedColor: UIColor.cloudsColor(), roundingCorners: corners)

//Fancy Buttons
cell.cornerRadius = 7
if indexPath.row == 0 {
cell.trailorButton.addTarget(self, action: "openHunger:", forControlEvents: UIControlEvents.TouchUpInside)
}else if indexPath.row == 1{
cell.trailorButton.addTarget(self, action: "openCreed:", forControlEvents: UIControlEvents.TouchUpInside)
}
cell.trailorButton.layer.cornerRadius = 5
cell.trailorButton.tintColor = UIColor.whiteColor()

let movieNameStr = movieTitle[indexPath.row] as! String
cell.movieImage.image = UIImage(named: movieNameStr)

//Cell Data Insertion

if (self.resultSearchController.active){
print("search ---------------")
cell.movieName.text = filteredData[indexPath.row].valueForKey("Title") as? String
cell.movieDetail.text = filteredData[indexPath.row].valueForKey("Time") as? String
return cell
}else {
print("cell")
if indexPath.section == 0 {
cell.movieName.text = movieSection1[indexPath.row].valueForKey("Title") as? String
cell.movieDetail.text = movieSection1[indexPath.row].valueForKey("Time") as? String
} else if indexPath.section == 1 {
cell.movieName.text = movieSection2[indexPath.row].valueForKey("Title") as? String
cell.movieDetail.text = movieSection2[indexPath.row].valueForKey("Time") as? String
}
return cell
}



//return cell
}

@IBAction func openHunger(sender: AnyObject) {
UIApplication.sharedApplication().openURL(NSURL(string: "https://youtu.be/n-7K_OjsDCQ")!)
}
@IBAction func openCreed(sender: AnyObject) {
UIApplication.sharedApplication().openURL(NSURL(string: "https://youtu.be/fCBzWLVQgk8")!)
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionInTable[section] as? String
}

}

任何帮助将不胜感激。谢谢

最佳答案

我错过了它,感谢 Larcerax,通过添加 mTableView.reloadData()

关于ios - UISearchBar 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34005634/

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