【问题标题】:I get the error "index 6 beyond bounds [0 .. 5]' " when implementing search in my app在我的应用程序中实现搜索时出现错误“索引 6 超出范围 [0 .. 5]”
【发布时间】:2016-05-23 14:47:57
【问题描述】:

这是我的代码。在遵循多个关于如何在 Swift 中实现搜索的教程时,我没有运气。

import UIKit

class DataTableExercisesTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {

var exercises = ["Abs", "Arms", "Back", "Chest", "Legs", "Shoulders", "Triceps"]
var searchActive : Bool = false

@IBOutlet weak var searchBar: UISearchBar!
var filteredTableData = [String]()
var resultSearchController = UISearchController()



override func viewDidLoad() {
    super.viewDidLoad()
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    // Reload the table
    self.tableView.reloadData()
}



// MARK: - Table view data source



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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return exercises.count;
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell;
    if (self.resultSearchController.active) {
        cell.textLabel?.text = filteredTableData[indexPath.row]

        return cell
    }
    else {
        cell.textLabel?.text = exercises[indexPath.row]

        return cell
    }

}

func updateSearchResultsForSearchController(searchController: UISearchController)
{
    filteredTableData.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (exercises as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredTableData = array as! [String]

    self.tableView.reloadData()
}




 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {




    self.tableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

我在执行不同教程中的搜索时遇到了麻烦,而且似乎效果不太好。非常感谢任何见解。

【问题讨论】:

    标签: swift uitableview tableview uisearchbar


    【解决方案1】:

    您的numberOfRowsInSection 总是返回exercises.count。但是在过滤时,您使用的不是exercises,而是一个较小的数组filteredTableData。因此,就像在 cellForRowAtIndexPath 中一样,如果您正在过滤,则需要更改答案。

    【讨论】:

    • 所以我需要更改 'filteredTableData' 来接受我的练习字符串?我该怎么做?
    • "所以我需要更改 'filteredTableData' 以接受我的练习字符串" 我不知道你在说什么,对不起。
    【解决方案2】:

    最好的解决方案是在访问数组值之前检查总计数应该小于您要从数组中获取的索引或使用以下方式来迭代数组

    前:

    let arrayOfInts: [Int] = [1, 2, 3];          
      for i in arrayOfInts {
          print(i);
      }
    

    在您的情况下,您可以更改代码:

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         var rowCount = 0
        if(self.resultSearchController.active){
          rowCount = filteredTableData.count
        }
        else{
          rowCount = exercises.count
        }
        return rowCount;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-14
      • 2018-04-10
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 2011-08-15
      相关资源
      最近更新 更多