didSelectRow在搜索处于活动状态时,indexPath函数不起作用

时间:2018-05-05 00:00:13

标签: ios arrays swift uialertview didselectrowatindexpath

我正在尝试进行搜索,然后点按其中一个单元格以显示AlertView。这个alertView要求添加到数组。我正在使用didSelectRowAt indexPath函数和搜索功能。在任何搜索之前,一切正常。但是如果搜索处于活动状态,则搜索栏工作正常,但是tapped单元格不会显示alertView。我认为didSelectRowAt indexPath函数有问题。

这是我的搜索功能:

    func updateSearchResults(for searchController: UISearchController) {
    if searchController.searchBar.text != "" {
        filteredData.removeAll(keepingCapacity: false)
        let textToSearch = searchController.searchBar.text!
        filteredData = mainArray.filter({($0.names?.lowercased().contains(textToSearch.lowercased()))!})
        listTableView.reloadData()
    }
    else {
        filteredData.removeAll(keepingCapacity: false)
        listTableView.reloadData()
    }
}

这是didSelectRow函数:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let alertController = UIAlertController(title: "Alert", message: "Add To array?", preferredStyle: .alert)
    let yesAction = UIAlertAction(title: "Yes", style: .default) { (action) -> Void in
        if self.resultSearchController.searchBar.text != "" {
            self.secondArray.append(self.filteredData[indexPath.row])
        }
        else {
            self.secondArray.append(self.mainArray[indexPath.row])
        }
        print(self.secondArray)
    }
    let noAction = UIAlertAction(title: "No", style: .default) { (action) -> Void in
        print("Do not append")
    }
    alertController.addAction(yesAction)
    alertController.addAction(noAction)
    if self.presentedViewController == nil {
        self.present(alertController, animated: true, completion: nil)
    }
}

这是cellForRowAt功能。我知道这看起来很复杂。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier: String = "BasicCell"
    let myCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MyTableCell
    //Label1
    if resultSearchController.searchBar.text != "" {
        let item: LocationModel = filteredData[indexPath.row]
        let desenNoToString = item.DesenNo
        let desenString = "\(desenNoToString)"
        let regex = try! NSRegularExpression(pattern:"\"(.*)\"")
        if let match = regex.firstMatch(
            in: desenString, range:NSMakeRange(0,desenString.utf16.count)) {
            let result = (desenString as NSString).substring(with: match.range(at:1))
            myCell.label1!.text = result
        }
        //Label2
        let dolarToString = item.Dolar
        let dolarString = "\(dolarToString)"
        if let match = regex.firstMatch(
            in: dolarString, range:NSMakeRange(0,dolarString.utf16.count)) {
            let result = (dolarString as NSString).substring(with: match.range(at:1))
            myCell.label2!.text = result
        }
        //Label3
        let zeminToString = item.Zemin
        let zeminString = "\(zeminToString)"
        if let match = regex.firstMatch(
            in: zeminString, range:NSMakeRange(0,zeminString.utf16.count)) {
            let result = (zeminString as NSString).substring(with: match.range(at:1))
            myCell.label3!.text = result
        }
        //Label4
        let renkToString = item.Renk
        let renkString = "\(renkToString)"
        if let match = regex.firstMatch(
            in: renkString, range:NSMakeRange(0,renkString.utf16.count)) {
            let result = (renkString as NSString).substring(with: match.range(at:1))
            myCell.label4!.text = result
        }
        //Label5
        let enToString = item.En
        let enString = "\(enToString)"
        if let match = regex.firstMatch(
            in: enString, range:NSMakeRange(0,enString.utf16.count)) {
            let result = (enString as NSString).substring(with: match.range(at:1))
            myCell.label5!.text = result
        }
        //Label6
        let euroToString = item.Euro
        let euroString = "\(euroToString)"
        if let match = regex.firstMatch(
            in: euroString, range:NSMakeRange(0,euroString.utf16.count)) {
            let result = (euroString as NSString).substring(with: match.range(at:1))
            myCell.label6!.text = result
        }
    }else {
        let item: LocationModel = feedItems[indexPath.row]
        let desenNoToString = item.DesenNo
        let desenString = "\(desenNoToString)"
        let regex = try! NSRegularExpression(pattern:"\"(.*)\"")
        if let match = regex.firstMatch(
            in: desenString, range:NSMakeRange(0,desenString.utf16.count)) {
            let result = (desenString as NSString).substring(with: match.range(at:1))
            myCell.label1!.text = result
        }
        //Label2
        let dolarToString = item.Dolar
        let dolarString = "\(dolarToString)"
        if let match = regex.firstMatch(
            in: dolarString, range:NSMakeRange(0,dolarString.utf16.count)) {
            let result = (dolarString as NSString).substring(with: match.range(at:1))
            myCell.label2!.text = result
        }
        //Label3
        let zeminToString = item.Zemin
        let zeminString = "\(zeminToString)"
        if let match = regex.firstMatch(
            in: zeminString, range:NSMakeRange(0,zeminString.utf16.count)) {
            let result = (zeminString as NSString).substring(with: match.range(at:1))
            myCell.label3!.text = result
        }
        //Label4
        let renkToString = item.Renk
        let renkString = "\(renkToString)"
        if let match = regex.firstMatch(
            in: renkString, range:NSMakeRange(0,renkString.utf16.count)) {
            let result = (renkString as NSString).substring(with: match.range(at:1))
            myCell.label4!.text = result
        }
        //Label5
        let enToString = item.En
        let enString = "\(enToString)"
        if let match = regex.firstMatch(
            in: enString, range:NSMakeRange(0,enString.utf16.count)) {
            let result = (enString as NSString).substring(with: match.range(at:1))
            myCell.label5!.text = result
        }
        //Label6
        let euroToString = item.Euro
        let euroString = "\(euroToString)"
        if let match = regex.firstMatch(
            in: euroString, range:NSMakeRange(0,euroString.utf16.count)) {
            let result = (euroString as NSString).substring(with: match.range(at:1))
            myCell.label6!.text = result
        }
    }
    return myCell
}
希望你们能帮助我。

编辑:还有问题。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

在DidSelectRowAtIndexPath,

不正确:

if self.presentedViewController == nil {
   self.present(alertController, animated: true, completion: nil) 
}

只需替换为:

self.present(alertController, animated: true, completion: nil)

说明 对于正常情况,当您在此时点击任何tableview单元格时,未显示任何控制器。所以" self.presentedViewController == nil"永远是真的。

但是,Whiles搜索任何值,UISearchbar Controller总是被呈现。所以情况总是错误的。

即使在呈现的控制器上,我们也可以呈现警报视图。所以不需要检查这种情况。