Hoşgeldin. Soru sormak veya cevaplamak için hemen üye ol.

question-closed UISearchbar'dan DetailsVC'ye geçerken index problemi

0 oy
14 kez görüntülendi
4, Ekim, 4 ios development kategorisinde cagataykyt tarafından soruldu
4, Ekim, 4 yasin tarafından kapalı
import UIKit
import Parse

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var activity: UIActivityIndicatorView!

var tableBaslikArray = [String]()
var tableMaddeArray = [String]()

var searchedMadde = [String]()
var searching = false


var chosenPlace = ""


override func viewDidLoad() {
    super.viewDidLoad()
    hideKeyboard()
    tableView.delegate = self
    tableView.dataSource = self
    searchBar.delegate = self


    //borderları yok etme
    self.searchBar.backgroundImage = UIImage()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for:.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()

    //Navbar Title Sola kaydırma
    let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - 32, height: view.frame.height))
    titleLabel.text = "  Türk Borçlar Kanunu"
    titleLabel.textColor = UIColor.white
    titleLabel.font = UIFont.systemFont(ofSize: 17)
    navigationItem.titleView = titleLabel


    getData()


    activity.isHidden = false
    activity.startAnimating()


}

override func viewDidAppear(_ animated: Bool) {

    if CheckInternet.Connection(){



    }

    else{

        self.Alert(Message: "Lütfen internet bağlantınızı kontrol edin.")
    }

}

func Alert (Message: String){

    let alert = UIAlertController(title: "Bağlantı yok", message: Message, preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "Tamam", style: UIAlertAction.Style.default, handler: nil))
    self.present(alert, animated: true, completion: nil)


}


func getData() {

    let query = PFQuery(className: "Avokadotbk")
    query.limit = 1000
    query.findObjectsInBackground { (objects, error) in



        self.tableMaddeArray.removeAll(keepingCapacity: false)
        self.tableBaslikArray.removeAll(keepingCapacity: false)

        for object in objects! {

            self.tableMaddeArray.append(object.object(forKey: "madde") as! String)
            self.tableBaslikArray.append(object.object(forKey: "baslik") as! String)

        }

        self.tableView.reloadData()
        self.activity.isHidden = true
        self.activity.stopAnimating()
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetails" {
        let destinationVC = segue.destination as! MaddeVC
        destinationVC.selectedPlace = self.chosenPlace

    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.chosenPlace = tableMaddeArray[indexPath.row]



   performSegue(withIdentifier: "showDetails", sender: self)

    tableView.deselectRow(at: indexPath, animated: true)

}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ViewControllerCell



        if searching {
            //cell.tableBaslik.text = searchedBaslik[indexPath.row]
            cell.tableMadde.text = "Madde \(searchedMadde[indexPath.row])"

        } else {
            cell.tableBaslik.text = tableBaslikArray[indexPath.row]
            cell.tableMadde.text = "Madde \(tableMaddeArray[indexPath.row])"
        }

    return cell
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    if searching {
        return searchedMadde.count
    } else {
        return tableMaddeArray.count
    }

}


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchedMadde = tableMaddeArray.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
    //searchedBaslik = tableBaslikArray.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
    searching = true
    tableView.reloadData()

    if searchBar.text == nil || searchBar.text == ""
    {
        searchBar.perform(#selector(self.resignFirstResponder), with: nil, afterDelay: 0.1)

    }
}

Kodum bu şekilde arama yaptığımda ilk indeksten başlıyor.

...