Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 我得到了这个错误;指数6超出界限[0..5]'&引用;在我的应用程序中实现搜索时_Swift_Uitableview_Tableview_Uisearchbar - Fatal编程技术网

Swift 我得到了这个错误;指数6超出界限[0..5]'&引用;在我的应用程序中实现搜索时

Swift 我得到了这个错误;指数6超出界限[0..5]'&引用;在我的应用程序中实现搜索时,swift,uitableview,tableview,uisearchbar,Swift,Uitableview,Tableview,Uisearchbar,这是我的密码。在学习如何在Swift中实现搜索的多个教程时,我运气不佳 import UIKit class DataTableExercisesTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating { var exercises = ["Abs", "Arms", "Back", "Chest", "Legs", "Shoulders", "Triceps"] var

这是我的密码。在学习如何在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.
}

我在实现不同教程中的搜索时遇到了问题,只是效果似乎不太好。非常感谢您的任何见解。

您的
行数部分
总是返回
练习。计数
。但是当您进行筛选时,您使用的不是
练习
,而是一个较小的数组,
filteredTableData
。因此,正如在
cellforrowatinexpath
中一样,如果要进行筛选,则需要更改答案。

最佳解决方案是在访问数组值之前,只需检查总计数是否应小于要从数组中获取的索引,或者使用下面的方法迭代数组

例:

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

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;
}

所以我需要更改“filteredTableData”以接受我的练习字符串?所以我需要更改'filteredTableData'来接受我的练习字符串“对不起,我不知道你在说什么。
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;
}