Abirami Bala
Abirami Bala

Reputation: 790

Search using TextFiled in Multi Sectioned TableView - Swift

I made a search with textfield with the below method and its working fine when i use single section in table view.

But i don’t know how to achieve this when i have multiple sections in table View

In Normal Single Sectioned tableview

foodTypeArr = ["Japanesh", "Chinesh", "Lebanesh", "Italian", "Indian"]
searchResultArr.addObjects(from: foodTypeArr as! [Any])

func searchAutocompleteEntriesWithSubstring(substring: String)
{
 searchResultArr.removeAllObjects()

        for curString in foodTypeArr
        {
            let myString:NSString! = curString as! NSString

            if (myString).lowercased.contains(substring)
            {
                searchResultArr.add(curString)
            }
        }

        self.tblList.reloadData()
 }

In Multi Section TableView

var searchAreaArr = NSMutableArray()    
var areaArr = [[String]]()
var areaTitles = NSArray()

areaArr = [["Vadvalli", "Mullai Nagar", "P.N.Pudhur"], ["Lawly Road", "Kowly Brown", "D.B Road"], ["Raja Street", "Gandhipark", "Five corner road”, "Main Town Hall”]]

areaTitles = ["Vadavalli", "R.S.Puram", "Town Hall"]

for i in 0..<areaArr.count
{
   searchAreaArr.addObjects(from: areaArr[i])
}

I’m calling the below method in TextField-shouldChangeCharactersIn range

func searchAutocompleteEntriesWithSubstring(substring: String)
{
    areaArr.removeAll()
            var mArr = [String]()

        for curString in searchAreaArr
        {
            let myString:NSString! = curString as! NSString

            if (myString).lowercased.contains(substring)
            {
                mArr.append(curString as! String)
            }
        }
        areaArr.insert(mArr, at: 0)
}

with this it is inserting the filtered Strings in the 0th array Index in areaArr. But I need it should be on their sections and the section name should be their own, and to change the section Index of the filtered sections of strings

Say For example if the search text is road, it should be

R.S puram (Section 1)
- Lawly Road
- D.B Road
Town Hall (Section 2)
- Five corner road

My tableview Methods

func numberOfSections(in tableView: UITableView) -> Int
{
    return areaArr.count
}

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

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    if section < areaTitles.count
        {
            return areaTitles[section] as? String
        }
    return nil
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
        let cell:UITableViewCell = 
self.tblList.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
    cell.textLabel?.text = "\(areaArr[indexPath.section][indexPath.row])"
}

I Lost my One whole day with it, Please Help to Fix this…. Your Suggestions are welcomed

Upvotes: 3

Views: 443

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15768

You can do something like this in shouldChangeCharactersIn method

let areaArr = [["Vadvalli", "Mullai Nagar", "P.N.Pudhur"], ["Lawly Road", "Kowly Brown", "D.B Road"], ["Raja Street", "Gandhipark", "Five corner road", "Main Town Hall"]]

let areaTitles = ["Vadavalli", "R.S.Puram", "Town Hall"]
let searchText = "road"
var areaArrResult = areaArr.map { $0.filter({ $0.lowercased().contains(searchText) }) }
let areaTitlesResult = areaTitles.filter {  !areaArrResult[areaTitles.index(of: $0)!].isEmpty }
areaArrResult = areaArrResult.filter { !$0.isEmpty }

print(areaArrResult)//[["Lawly Road", "D.B Road"], ["Five corner road"]]
print(areaTitlesResult)//["R.S.Puram", "Town Hall"]

OR

     var allDetails:[(String,[String])] = [] 
     var searchResult:[(String,[String])] = []


    allDetails = [("Vadavalli",["Vadvalli", "Mullai Nagar", "P.N.Pudhur"]),
                  ("R.S.Puram",["Lawly Road", "Kowly Brown", "D.B Road"]),
                  ("Town Hall",["Raja Street", "Gandhipark", "Five corner road", "Main Town Hall"])]
    searchAreaArr = areaArr.map({ ($0.0,$0.1.filter({ $0.lowercased().contains(substring) })) }).filter { !$0.1.isEmpty}
    print(searchResult)//[("R.S.Puram", ["Lawly Road", "D.B Road"]), ("Town Hall", ["Five corner road"])]

Then In numberOfSections return searchResult.count

In numberOfRowsInScetion return searchResult[section].1.count

In titleForHeaderInSection method you can use

return searchResult[section].0

In cellForRowAt method

cell.textLabel?.text = "\(searchResult[indexPath.section].1[indexPath.row])"

Upvotes: 3

Related Questions