searchBar函数但TableView发生致命错误

时间:2018-08-14 14:00:32

标签: swift

我在tableview中构建了搜索栏,一切正常,Search功能都很好,如果我在searchBar上输入了文字,则可以在tableView中正确显示搜索结果。 但这是问题所在:第一次运行此应用程序时,直接在tableview上按会出现致命错误。问题仅在我第一次运行该应用程序且未在searchBar中编写任何内容并直接在tableview菜单上按时出现。我有点困惑,下面您可以看到我的代码。如何解决?预先感谢。

enter image description here

导入UIKit

var searchController:UISearchController=UISearchController()

@IBOutlet weak var TableView: UITableView!

@IBOutlet var infoView: UIView!

@IBOutlet weak var infoImage: UIImageView!

@IBOutlet weak var infoNameLabel: UILabel!

@IBOutlet weak var dimView: UIView!


@IBAction func closeInfoPopup(_ sender: Any) {

    UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
        self.dimView.alpha = 0
        self.infoView.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
    }) { (success) in
        self.infoView.removeFromSuperview()
    }
}

 override func viewDidLoad() {
    super.viewDidLoad()
    configureSearchController()

    Data.getData { (data) in
        self.tableData=data
        self.TableView.reloadData()

    }
    TableView.delegate=self
    TableView.dataSource=self
}

func updateSearchResults(for searchController: UISearchController) {

        if let searchText = searchController.searchBar.text, !searchText.isEmpty {
            filteredArray = tableData.filter { data in
                return data.name.lowercased().contains(searchText.lowercased())
            }

        } else {
            filteredArray = tableData
        }
        TableView.reloadData()
    }

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    if !shouldShowSearchResults{
        shouldShowSearchResults = true
        TableView.reloadData()
    }
    searchController.searchBar.resignFirstResponder()
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    shouldShowSearchResults = true
    TableView.reloadData()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    shouldShowSearchResults = false
    TableView.reloadData()
}

func configureSearchController(){
    searchController=UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater=self
    searchController.dimsBackgroundDuringPresentation=false
    searchController.searchBar.placeholder="Search here..."
    searchController.searchBar.delegate=self
    searchController.searchBar.sizeToFit()

    TableView.tableHeaderView = searchController.searchBar
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let data=tableData[indexPath.row]
    let filteredData=filteredArray[indexPath.row]

    if shouldShowSearchResults == true{
        infoImage.image=filteredData.image
        infoNameLabel.text=filteredData.name
        infoView.center=view.center
        view.addSubview(infoView)
    }else{
        infoImage.image=data.image
        infoNameLabel.text=data.name
        infoView.center=view.center
        view.addSubview(infoView)
    }
    infoView.transform = CGAffineTransform(scaleX: 0.8, y: 1.2)
    UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
        self.dimView.alpha=0.8
        self.infoView.transform = .identity
    })
    }




func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 160
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if shouldShowSearchResults{
        return filteredArray.count
    }else{
        return tableData.count
    }

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell=tableView.dequeueReusableCell(withIdentifier: "cell") as! XTableViewCell

    if shouldShowSearchResults{
        cell.setup(modeleData: filteredArray[indexPath.row])
    }else{
        cell.setup(modeleData: tableData[indexPath.row])
    }

    return cell
}

}

1 个答案:

答案 0 :(得分:1)

您应该将didSelectRowAt方法更新为此,因为最初在filteredData中没有对象,并且您试图从中获取indexPath.row中的数据,这导致崩溃。

let data=tableData[indexPath.row]
let filteredData=filteredArray[indexPath.row]

这两行应移至特定条件。看下面的代码。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if shouldShowSearchResults == true{
    let filteredData=filteredArray[indexPath.row]

    infoImage.image=filteredData.image
    infoNameLabel.text=filteredData.name
    infoView.center=view.center
    view.addSubview(infoView)
}else{
    let data=tableData[indexPath.row]

    infoImage.image=data.image
    infoNameLabel.text=data.name
    infoView.center=view.center
    view.addSubview(infoView)
}
infoView.transform = CGAffineTransform(scaleX: 0.8, y: 1.2)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
    self.dimView.alpha=0.8
    self.infoView.transform = .identity
})
}
}