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