我有一个设置了UISearchBar的tableView。每次用户开始在搜索栏中输入时,应用程序都会崩溃。我发现问题在于我使用自定义tableViewCell(当我尝试使用默认的tableViewCell运行应用程序时,它没有崩溃并且工作正常。)。有想法该怎么解决这个吗?感谢。
这是我的代码:
import UIKit
import CoreData
class KeepTableViewController: UITableViewController, UISearchBarDelegate{
@IBOutlet weak var searchBar: UISearchBar!
var filteredQuotes = [AnyObject]()
var keptQuotes = [NSManagedObject]()
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
searchBar.showsScopeBar = true
tableView.rowHeight = UITableViewAutomaticDimension
getCoreData()
searchDisplayController?.searchResultsTableView.registerClass(QuoteyTableViewCell.self, forCellReuseIdentifier: "Cell")
}
func getCoreData(){
var appDel : AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var context : NSManagedObjectContext = appDel.managedObjectContext!
var req : NSFetchRequest = NSFetchRequest(entityName: "KeptQuotes")
var error : NSError?
let fetchedResults = context.executeFetchRequest(req, error: &error) as [NSManagedObject]?
if let results = fetchedResults {
keptQuotes = results
}else{
println("Could not fetch \(error), \(error!.userInfo)")
}
tableView.reloadData()
}
@IBAction func cancelPressed(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil) //dismisses the freakin view
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if tableView == self.searchDisplayController?.searchResultsTableView {
return filteredQuotes.count
}else{
return keptQuotes.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : QuoteyTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as QuoteyTableViewCell
var entry : NSManagedObject
if tableView == self.searchDisplayController!.searchResultsTableView{
entry = filteredQuotes[indexPath.row] as NSManagedObject
}else{
entry = keptQuotes[indexPath.row] as NSManagedObject!
}
cell.authorLabel.text = entry.valueForKey("author") as String!
cell.quoteTextLabel.text = entry.valueForKey("quote") as String!
cell.quoteTextLabel.sizeToFit()
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
func filterContentForSearchText(searchText: String) {
var qs : NSArray = keptQuotes
let predicate = NSPredicate(format: "quote contains[c] %@ OR author contains[c] %@", searchText, searchText)
filteredQuotes = qs.filteredArrayUsingPredicate(predicate!)
println(filteredQuotes)
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
self.filterContentForSearchText(searchDisplayController!.searchBar.text)
return true
}
}
自定义TableViewCell:
import UIKit
class QuoteyTableViewCell: UITableViewCell {
@IBOutlet weak var authorLabel: UILabel!
@IBOutlet weak var quoteTextLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
quoteTextLabel.textColor = UIColor(rgba: "#293B50")
authorLabel.textColor = UIColor(rgba: "#A4ACB5")
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
答案 0 :(得分:1)
我不久前也遇到了和你一样的问题。我做的工作不是直接在原型单元中布置我的自定义单元格,而是为它创建了一个单独的.xib
文件,将我tableViewController
中的原型单元格设置为0并执行< / p>
let regularCell = UINib(nibName: "Cell", bundle: nil)
self.searchDisplayController!.searchResultsTableView.registerNib(regularCell, forCellReuseIdentifier: "Cell")