Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Ios UITableView可以';t防止数组索引超出范围-Swift_Ios_Arrays_Swift_Uitableview - Fatal编程技术网

Ios UITableView可以';t防止数组索引超出范围-Swift

Ios UITableView可以';t防止数组索引超出范围-Swift,ios,arrays,swift,uitableview,Ios,Arrays,Swift,Uitableview,如您所见,我正在使用搜索功能过滤表视图的主数组 func numberOfSectionsInTableView(tableView: UITableView) -> Int { if(searchActive){ return filteredRooms.count} return roomlist.count } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -

如您所见,我正在使用搜索功能过滤表视图的主数组

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    if(searchActive){ return filteredRooms.count}
    return roomlist.count

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(searchActive){ return filteredRooms[section].count }
    return roomlist[section].count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let sec = indexPath.section
    let row = indexPath.row

    let cell = tableView.dequeueReusableCellWithIdentifier("RoomCell", forIndexPath: indexPath) as! UITableViewCell

    var room:RoomItem;

    if(searchActive){

        room = filteredRooms[sec][row]  // Array out of bounds here

    }else{

        room = roomlist[sec][row]

    }
那么,
numberOfSectionsInTableView
是否应该先运行,然后运行
numberofrowsinssection
,然后运行
cellforrowatinexpath

在这种情况下,不应该抛出错误,我不可能得到一个数组越界错误

如果函数未按该顺序运行,如何防止错误

编辑:添加搜索激活码

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    filteredRooms = filterz(roomlist, txt: searchText)

    if(filteredRooms.count == 0){
        searchActive = false
    } else {
        searchActive = true
        println("Activating search")
    }


    self.tViewRooms.reloadData()
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    searchActive = true;
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    searchActive = false;
}

在设置
searchActive
@sylvanaar
reloadData
后是否调用
reloadData
在搜索栏中的任何文本更改后是否调用您的代码您的代码中的其他地方一定有错误。在设置并重置Bool的位置添加代码searchActive@MayurDeshmukh添加了代码