我在很多问题之后设法将我的搜索栏实现到我的表视图中。实际的搜索本身就是我能说的,但是结果没有正确显示,因为我还没有设法添加' cellForRowAtIndexPath'搜索代码。
这是我关注的教程:http://www.appcoda.com/search-bar-tutorial-ios7/
我在尝试将一些代码行转换为Swift时遇到了问题。例如,我不知道Swift中的这一行是什么,也不知道它用于什么:
Recipe *recipe = nil;
这是我目前的代码:
func filterContentForSearchText(searchText: NSString) {
let resultPredicate = NSPredicate(format: "SELF contains[cd] %@", searchText)
searchResults = arrayDataPayments.filteredArrayUsingPredicate(resultPredicate)
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
println(arrayObject.paymentsArray().count)
if tableView == self.searchDisplayController.searchResultsTableView {
return searchResults.count
} else {
if arrayObject.paymentsArray().count == 0 {
if enterButtonTapped == false {
backgroundLabel.text = "Before you add any transactions, you must first set a budget. You can do this by tapping the 'Budget' tab."
} else {
backgroundLabel.text = "You haven't added any transactions yet. Tap the add button to add a new transaction."
}
backgroundLabel.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
backgroundLabel.numberOfLines = 0
backgroundLabel.textAlignment = NSTextAlignment.Center
backgroundLabel.sizeToFit()
backgroundLabel.hidden = false
backgroundLabel.textColor = UIColor.lightGrayColor()
self.tableView.backgroundView = backgroundLabel
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
self.tableView.scrollEnabled = false
return 0
} else {
backgroundLabel.hidden = true
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
self.tableView.scrollEnabled = true
return arrayObject.paymentsArray().count
}
}
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:CustomTransactionTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
cell.paymentNameLabel.text = (arrayObject.paymentsArray().objectAtIndex(indexPath.row)) as String
cell.costLabel.text = (arrayObject.costArray().objectAtIndex(indexPath.row)) as String
cell.dateLabel.text = (arrayObject.dateArray().objectAtIndex(indexPath.row)) as String
if arrayObject.imageArray().objectAtIndex(indexPath.row) as NSObject == 0 {
cell.paymentArrowImage.hidden = false
cell.creditArrowImage.hidden = true
} else if arrayObject.imageArray().objectAtIndex(indexPath.row) as NSObject == 1 {
cell.creditArrowImage.hidden = false
cell.paymentArrowImage.hidden = true
}
return cell
}
override func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
if let tv=tableView {
var valueToRemove: AnyObject! = arrayDataValue.objectAtIndex(indexPath!.row)
println(valueToRemove)
if arrayObject.imageArray().objectAtIndex(indexPath.row) as NSObject == 0 {
totalSpendingsCounter = totalSpendingsCounter - Double(valueToRemove as NSNumber)
} else if arrayObject.imageArray().objectAtIndex(indexPath.row) as NSObject == 1 {
totalCreditCounter = totalCreditCounter - Double(valueToRemove as NSNumber)
}
arrayDataCost.removeObjectAtIndex(indexPath!.row)
arrayDataImage.removeObjectAtIndex(indexPath!.row)
arrayDataPayments.removeObjectAtIndex(indexPath!.row)
arrayDataDate.removeObjectAtIndex(indexPath!.row)
arrayDataValue.removeObjectAtIndex(indexPath!.row)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
newTransactionEntered = true
}
}
}
如果有人能指出我正确的方向,那么我将非常感激。这是我的第一个应用程序的最后阶段之一,它是我发现最难的部分。
答案 0 :(得分:0)
您使用相同的tableView
来显示搜索结果,这不是解决此问题的最佳方法。
创建一个新的UITableViewController
子类,然后你可以这样做:
searchResultsViewController = self.storyboard.instantiateViewControllerWithIdentifier("SearchResults") as? MySearchResultViewController
searchController = UISearchController(searchResultsController: searchResultsViewController)
searchController!.dimsBackgroundDuringPresentation = true
searchController!.searchResultsUpdater = searchResultsViewController
您的MySearchResultViewController
应符合UISearchResultsUpdating
协议并实施updateSearchResultsForSearchController:
。当您开始搜索时,UIKit
会自动显示您的searchResultsViewController
,并负责将其视图添加到层次结构中,并在必要时将其删除。
此外,在一个函数中设置tableView
属性并不是一个好习惯,它的工作只是返回numberOfRowsInSection
。