1 Replies - 1168 Views - Last Post: 16 June 2019 - 12:17 PM

#1 Yambee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-March 19

Get correct info from table.

Posted 01 June 2019 - 12:08 PM

I've got a question... How do I get the correct info from the rows in my table after I do a search?

My app has a search bar and a table.
When the app starts some info is loaded into the table.
When I type some words into the search bar, that info is reorganized.
And when I try to get the (newly located) info from the row, it shows the default info instead.
Here's the code..
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    
    let CellIdentifier = "MyFirstTableViewCell"

    let data = [
    "A1",
    "A2",
    "A3",
    "A4",
    "A5",
    "B1",
    "B2",
    "B3",
    "B4",
    "B5",
    "C1",
    "C2",
    "C3",
    "C4",
    "C5",
    ]

    var filteredData: [String]!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: CellIdentifier)
    
     self.tableView.delegate = self
    
        searchBar.delegate = self
        filteredData = data
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as UITableViewCell
        cell.textLabel?.text = filteredData[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredData.count
    }

    // This method updates filteredData based on the text in the Search Box
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        // When there is no text, filteredData is the same as the original data
        // When user has entered text into the search box
        // Use the filter method to iterate over all items in the data array
        // For each item, return true if the item should be included and false if // the item should NOT be included
        filteredData = searchText.isEmpty ? data : data.filter { (item: String) -> Bool in
            // If dataItem matches the searchText, return true to include it
            return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
        }
        
        tableView.reloadData()
   
    }
	override func viewWillAppear(_ animated: Bool) {
	tableView.reloadData()
	}

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let theinfo = data[indexPath.row]
        print("Index is \(indexPath.row)" + " - " + "\(theinfo)")
        }
}


FYI - I'm not a good programmer, I'm completely new to Swift / xcode and it's taken me a LONG time to even get this far (which is mostly a cut and paste job from the net). So, if you reply, please keep in mind that you're talking to the local village idiot.
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Get correct info from table.

#2 DonSandbekkhaug   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 16-June 19

Re: Get correct info from table.

Posted 16 June 2019 - 12:17 PM

View PostYambee, on 01 June 2019 - 12:08 PM, said:

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let theinfo = data[indexPath.row]
        print("Index is \(indexPath.row)" + " - " + "\(theinfo)")
        }
}



Here, you're reading from data instead of from filteredData. You can add a property to the class such as 'var filteredData'. Then in tableView:didSelectRowAtIndexPath:, do this:

let theInfo = filteredData[indexPath.row]



Hope this helps!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1