我一直在尝试使用复选标记在UITableView上做SearchBar。用户一次只能勾选一个标记。我设法做复选标记和搜索。但是,当我搜索和搜索结果对号,这是它开始失败快速UItableview与搜索栏和复选标记
import UIKit
class CandyTableViewController : UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var lastSelectedIndexPath = NSIndexPath(forRow: 0, inSection: 0)
var candies = [Candy]()
var mySelected=String()
var filteredCandies = [Candy]()
override func viewDidLoad() {
self.candies = [Candy(category:"Chocolate", name:"chocolate Bar"),
Candy(category:"Chocolate", name:"chocolate Chip"),
Candy(category:"Chocolate", name:"dark chocolate"),
Candy(category:"Hard", name:"lollipop"),
Candy(category:"Hard", name:"candy cane"),
Candy(category:"Hard", name:"jaw breaker"),
Candy(category:"Other", name:"caramel"),
Candy(category:"Other", name:"sour chew"),
Candy(category:"Other", name:"gummi bear")]
mySelected=candies[lastSelectedIndexPath.row].name
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredCandies.count
} else {
return self.candies.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.accessoryType = .Checkmark
var candy : Candy
if tableView == self.searchDisplayController!.searchResultsTableView {
candy = filteredCandies[indexPath.row]
} else {
candy = candies[indexPath.row]
}
if mySelected==candy.name {
cell.accessoryType = .Checkmark
lastSelectedIndexPath=indexPath
} else {
cell.accessoryType = .None
}
cell.textLabel.text = candy.name
return cell
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
self.filteredCandies = self.candies.filter({(candy : Candy) -> Bool in
var categoryMatch = (scope == "All") || (candy.category == scope)
var stringMatch = candy.name.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String
self.filterContentForSearchText(searchString, scope: selectedScope)
return true
}
func searchDisplayController(controller: UISearchDisplayController!,
shouldReloadTableForSearchScope searchOption: Int) -> Bool {
let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text, scope: scope[searchOption])
return true
}
func searchDisplayController(controller: UISearchDisplayController, willHideSearchResultsTableView tableView: UITableView) {
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if indexPath.row != lastSelectedIndexPath?.row {
if let lastSelectedIndexPath = lastSelectedIndexPath {
let oldCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath)
oldCell?.accessoryType = .None
}
let newCell = tableView.cellForRowAtIndexPath(indexPath)
newCell?.accessoryType = .Checkmark
lastSelectedIndexPath = indexPath
mySelected = newCell?.textLabel.text ?? ""
}
if candies[indexPath.row].name != candies[lastSelectedIndexPath.row].name {
let oldCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath)
oldCell?.accessoryType = .None
}
let newCell = tableView.cellForRowAtIndexPath(indexPath)
newCell?.accessoryType = .Checkmark
lastSelectedIndexPath = indexPath
mySelected = newCell?.textLabel.text ?? ""
}
}
我希望它能够搜索后对号。并在返回完整列表时保持这种状态。请帮忙,我已经尝试过几天解决这个问题了。
更新:我设法按照预期完成。但相信这些代码可能会比这更简单
谢谢你。真的行。感谢你的帮助。只要这行代码xcode需要它就像这样mySelected = newCell?.textLabel.text? “”没有'?'在文本标签上。我仍然对如何使用可选值感到困惑。 – SyamAhmad 2014-11-24 22:08:36
我们可能会在不同的Xcode版本 - 似乎他们仍然来回地说什么属性应该是可选的而不是... – 2014-11-24 22:20:41
我注意到,当我们在搜索后选择与上一行相同的行,复选标记不会发生甚至不同文本。我想我的逻辑'tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath)'仍然不正确 – SyamAhmad 2014-11-25 07:03:37