如您所见,我正在使用搜索功能过滤表格视图的主数组
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
先跑,然后numberOfRowsInSection
,然后cellForRowAtIndexPath
??
如果是这种情况,则不应抛出任何错误,我无法获得数组越界错误。
如果功能未按该顺序运行,我该如何防止错误?
编辑:添加了搜索激活码
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;
}