Я пытаюсь выполнить поиск и коснитесь одной из ячеек, чтобы отобразить AlertView. И этот alertView просит добавить в массив. Я использую функцию didSelectRowAt indexPath
и функцию поиска. Перед любым поиском все отлично работает. Но если поиск активен, панель поиска работает нормально, но коснувшаяся ячейка не отображает 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
}
Надеюсь, вы, ребята, можете мне помочь.
Редактировать: Все еще есть проблема. Есть предложения?