我试图在我的应用程序中实现“搜索”,效果很好,但是如果您点击搜索栏,什么都不输入,然后点击清除按钮或点击搜索结果和“后退”按钮,然后“数组索引超出范围错误”出现。
import UIKit
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!
var filteredCats = [Cats]()
var searchActive : Bool = false
func loadList(notification: NSNotification){
//load data here
self.tableView.reloadData()
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)
// Установка параметров поиска
searchBar.tintColor = UIColor.whiteColor()
let textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
//Настраиваем отображение ячеек таблицей
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 215.0
tableView.separatorStyle = .None
print(filteredCats.count, searchActive)
// Do any additional setup after loading the view.
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false
dismissKeyboard()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
dismissKeyboard()
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filteredCats = catsArray.filter({ (Cats) -> Bool in
let tmp: NSString = Cats.title
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
if(filteredCats.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Считаем количество котов
if(searchActive) {
return self.filteredCats.count
} else {
return catsArray.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("catCell") as! TableViewCell
var catTitle = String()
var catImage = String()
if(searchActive){
catTitle = filteredCats[indexPath.row].title
catImage = filteredCats [indexPath.row].icon
} else {
catTitle = catsArray[indexPath.row].title
catImage = catsArray[indexPath.row].icon
}
if favorite.contains(catsArray[indexPath.row].index) {
cell.setCell(catTitle, imageName: "\(catImage)-1", buttonImage: containsInFavorite!)
cell.selectionStyle = UITableViewCellSelectionStyle.None
} else {
cell.setCell(catTitle, imageName: "\(catImage)-1", buttonImage: dontContainsInFavorite!)
}
cell.favoriteButton.tag = indexPath.row
cell.favoriteButton.addTarget(self, action: "switchFavorite:", forControlEvents: .TouchUpInside)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
print("showDetail seque called")
let detailView = segue.destinationViewController as! DetailViewController
detailView.hidesBottomBarWhenPushed = true
let backItem = UIBarButtonItem()
backItem.title = "Back"
navigationItem.backBarButtonItem = backItem
if filteredCats.count != 0 {
if let catIndex = tableView.indexPathForSelectedRow?.row {
let finalIndex = filteredCats[catIndex].index
detailView.catInfoIndex = finalIndex
print(catIndex, filteredCats.count)
}
} else {
if let catIndex = tableView.indexPathForSelectedRow?.row {
detailView.catInfoIndex = catIndex
print(catIndex, filteredCats.count)
}
}
}
}
override func viewWillAppear(animated: Bool) {
print(filteredCats.count, searchActive)
}
}
最佳答案
感谢您的帮助。 我解决了这个问题。 碰巧当我完成编辑后,“searchActive”var 变为“false”,如果之后我在 SearchBar 的 UITesxtField 中单击“清除”按钮,然后调用 tableView.reload(),并且只有在“searchActive”变为“true”之后,这就是为什么 'func tableView numberOfRowsInSection' 和 'func tableView cellForRowAtIndexPath' 工作不当并导致出现 'index out of array range' 错误。
通过在此方法中再添加一个“if...else”来解决此问题:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Считаем количество котов в библиотеке
if(searchActive) {
if filteredCats.count == 0{
return catsArray.count
} else {
return self.filteredCats.count
}
} else {
return catsArray.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("catCell") as! TableViewCell
//Устанавливаем параметры ячеек
cell.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
cell.selectionStyle = UITableViewCellSelectionStyle.None
var catTitle = String()
var catImage = String()
if(searchActive){
if filteredCats.count == 0{
catTitle = catsArray[indexPath.row].title
catImage = catsArray[indexPath.row].icon
} else {
catTitle = filteredCats[indexPath.row].title
catImage = filteredCats [indexPath.row].icon
}
} else {
catTitle = catsArray[indexPath.row].title
catImage = catsArray[indexPath.row].icon
}
关于arrays - 数组索引超出搜索范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34436703/