๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/iOS

[iOS] ๊ฒ€์ƒ‰ํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•œ ๊ธ€์ž ํ•„ํ„ฐ๋งํ•œ ๊ฐ์ฒด ํ…Œ์ด๋ธ”๋ทฐ์— ๋„์šฐ๊ธฐ(Search Object Filtering)

by Fomagran ๐Ÿ’ป 2021. 2. 25.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์–ธ์ œ๋‚˜ ๊ทธ๋žฌ๋“ฏ ์ œ๋ชฉ ์ •ํ•˜๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ํž˜๋“œ๋„ค์š”...

 

์˜ค๋Š˜์€ ์•Œ์•„๋ณผ๊ฑด ์„œ์น˜๋ฐ”์— ๊ฒ€์ƒ‰ํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•œ ๊ธ€์ž๊ฐ€ ํฌํ•จ๋œ ๊ฒƒ๋“ค์„ ํ•„ํ„ฐ๋งํ•œ ๊ฒƒ์„ ํ…Œ์ด๋ธ”๋ทฐ์— ๋„์šฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์ด๋ฒˆ ๊ธ€์€ ์ €๋ฒˆ ์‹œ๊ฐ„์— ์—‘์…€ ํŒŒ์‹ฑํ•˜๋Š”๋ฒ•์—์„œ ๋ฐฐ์› ๋˜ ๋‚˜๋ผ์™€ ๋„์‹œ ์ด๋ฆ„๋“ค์ด ๋‹ด๊ธด ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํ˜น์‹œ ๋ชป๋ณด์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ์—์„œ ๋ณด๊ณ ์™€์ฃผ์„ธ์š”!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Preview

 


Storyboard

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ…Œ์ด๋ธ”๋ทฐ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

(์ฃผ์˜ํ•˜์‹ค ์ ์€ ๋ฐ˜๋“œ์‹œ UIViewController๋กœ ๋งŒ๋“œ์‹  ํ›„์— TableView๋ฅผ AutoLayoutํ•ด์ฃผ์„ธ์š”!)

 

๊ทธ๋ฆฌ๊ณค ํ•ด๋‹น ํ…Œ์ด๋ธ”๋ทฐ๋ฅผ control์„ ๋ˆ„๋ฅด๊ณ  ๋“œ๋ž˜๊ทธํ•ด์„œ ViewController์— ๊ฐ€์ ธ๊ฐ€์„œ dataSource๋ฅผ ์ฒดํฌํ•ด์ฃผ์„ธ์š”.


AddViewController

 

์ œ์ผ ๋จผ์ € ํ…Œ์ด๋ธ”๋ทฐ๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ์„ธ์š”.

@IBOutlet weak var tableView: UITableView!

์•„๋ž˜์™€ ๊ฐ™์ด ๋ชจ๋“  ์œ„์น˜๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด๊ณผ ํ•„ํ„ฐ๋ง๋œ ์œ„์น˜๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 var allLocations:[Location] = []
 var filteredLocation:[Location] = []

Location ๊ตฌ์กฐ์ฒด๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋„์‹œ์™€ ๋‚˜๋ผ ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋ผ์ฝ”๋“œ๋ฅผ ํ”„๋กœํผํ‹ฐ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

struct WeatherLocation: {
    var city:String!
    var country:String!
    var countryCode:String!
}

 

๊ทธ ๋‹ค์Œ์œผ๋กœ ํ…Œ์ด๋ธ”๋ทฐ์˜ ์œ— ๋ถ€๋ถ„์— ํ•ด๋‹น๋˜๋Š” UISearchController๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

 let searchController = UISearchController(searchResultsController: nil)

 

 

๊ทธ๋ฆฌ๊ณค ์•„๋ž˜์™€ ๊ฐ™์ด setUpSearchController ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์„œ์น˜์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์„ธํŒ…ํ•ด๋ณผ๊ฒŒ์š”!

 

์ œ์ผ ๋จผ์ € ์œ„์—์„œ ์—ฐ๊ฒฐํ•œ ํ…Œ์ด๋ธ”๋ทฐ์˜ ํ—ค๋”์— searchController.searchBar๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

๊ทธ ๋‹ค์Œ์œผ๋กœ ์„œ์น˜๋ฐ”์˜ ํ”Œ๋ ˆ์ด์Šคํ™€๋”๋ฅผ ์ •ํ•ด์ฃผ๋Š”๋ฐ์š”.( ํ”Œ๋ ˆ์ด์Šคํ™€๋”๋Š” ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์ „ ์„œ์น˜๋ฐ”์— ํšŒ์ƒ‰๊ธ€์”จ๋กœ ๋ฏธ๋ฆฌ ๋„์›Œ์ฃผ๋Š” ๊ธ€์ž์ž…๋‹ˆ๋‹ค.)

       private func setUpSearchController() {
        
           tableView.tableHeaderView = searchController.searchBar
        
           searchController.searchBar.placeholder = "City or Country"
           searchController.searchResultsUpdater  = self
           searchController.obscuresBackgroundDuringPresentation = false
           searchController.searchBar.searchBarStyle = UISearchBar.Style.prominent
           searchController.searchBar.sizeToFit()
       }

๊ทธ๋ฆฌ๊ณ  ์ค‘์š”ํ•œ๊ฑด searchResultUpdater์ธ๋ฐ์š”. 

 

์ด๊ฒƒ์€ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰ํ•œ ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ปจํ…์ธ ๋ฅผ ์—…๋ฐ์ดํŠธ ์‹œ์ผœ์ฃผ๋Š” ํ”„๋กœํผํ‹ฐ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  obobscuresBackgroundDuringPresentation ๊ฐ€ false๋กœ ๋˜์–ด์žˆ๋Š”๋ฐ ์ด๊ฑด ๋ฌด์—‡์ผ๊นŒ์š”?

 

์• ํ”Œ ๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋Š” ๋‚ด์šฉ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด์„ํ•ด๋ณด๋ฉด 

 

"๊ฒ€์ƒ‰ ์ค‘์— ๊ธฐ๋ณธ ์ฝ˜ํ…์ธ ๊ฐ€ ๊ฐ€๋ ค ์ง€๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ €๋Š” ์ฒ˜์Œ์—” ์ดํ•ด๊ฐ€ ์•ˆ๋ผ์„œ ํ•œ๋ฒˆ true๋กœ ํ•ด๋ณด๊ณ  false๋กœ๋„ ํ•ด๋ณด์•˜๋Š”๋ฐ์š”.

 

์™ผ์ชฝ์ด true๋กœ ํ–ˆ์„๋•Œ ์˜ค๋ฅธ์ชฝ์ด false๋กœ ํ–ˆ์„๋•Œ ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณด๋‹ˆ ์ปจํ…์ธ ๋ฅผ ๊ฐ€๋ฆฐ๋‹ค๋Š” ๋ง์ด ๋ฐ”๋กœ ์ดํ•ด๊ฐ€ ๊ฐ€๋„ค์š”!

 

๊ทธ ๋‹ค์Œ์œผ๋ก  ์„œ์น˜๋ฐ”์˜ ์Šคํƒ€์ผ๊ณผ ์‚ฌ์ด์ฆˆ๋ฅผ ์›ํ•˜์‹œ๋Š”๋Œ€๋กœ ์ •ํ•ด์ฃผ์„ธ์š”!


AddTableViewCell

 

์•„๋ž˜์™€ ๊ฐ™์ด TableViewCell์„ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”!

class AddTableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
}


DataSource

 

์ด์ œ ํ…Œ์ด๋ธ”๋ทฐ์˜ DataSource๋ฅผ ์„ธํŒ…ํ•ด๋ณผ๊ฒŒ์š”.

 

๋จผ์ € numberOfRowsInSection์€ ํ•„ํ„ฐ๋ง๋œ  ์œ„์น˜์˜ ๊ฐฏ์ˆ˜๋งŒํผ ์„ธํŒ…ํ•ด์ฃผ์‹œ๊ณ 

 

์…€์˜ ์œ„ ๋ ˆ์ด๋ธ”์€ location์˜ ๋„์‹œ์ด๋ฆ„ ์•„๋žซ ๋ถ€๋ถ„์€ ๋‚˜๋ผ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

  extension AddViewController:UITableViewDataSource {
       func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           filteredLocation.count
       }
       
       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           
           let cell = tableView.dequeueReusableCell(withIdentifier: "AddTableViewCell") as! AddTableViewCell
           
           let location = filteredLocation[indexPath.row]
           cell.textLabel?.text = location.city
           cell.detailTextLabel?.text = location.country
           return cell
       }
   }

๊ทธ๋ฆฌ๊ณค UISearchResultsUpdating ๋”œ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์„ค์ •ํ•ด์ฃผ๋Š”๋ฐ

 

๋จผ์ € updateSearchResults๋Š” ๊ธฐ๋ณธ์œผ๋กœ ์„ค์ •๋˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.

 

์ด๊ฑด SearchController์˜ ๊ธ€์ž๊ฐ€ ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์—์š”!

 

๊ทธ๋Ÿฌ๋ฉด ๊ธ€์ž๊ฐ€ ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค filteredContentfilteredContentForSearchText ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๋Š” ๊ฒƒ์ด์ฃ .

 

filteredContentfilteredContentForSearchText ๋Š”  ๊ฒ€์ƒ‰ํ•œ ๊ธ€์ž๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์„œ 

 

๊ทธ ๊ธ€์ž๊ฐ€ ํฌํ•จ๋œ ๊ฐ์ฒด๋ฅผ ํ•„ํ„ฐ๋งํ•ด์„œ filteredLocation์— ๋‹ด์€ ๋’ค ํ…Œ์ด๋ธ”๋ทฐ๋ฅผ reload์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. 

 

๊ทธ๋Ÿฌ๋ฉด ๊ฒ€์ƒ‰ํ•œ ๊ธ€์ž๊ฐ€ ํฌํ•จ๋œ ๊ฐ์ฒด๋“ค๋งŒ ํ•„ํ„ฐ๋ง๋œ ์œ„์น˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด์— ์žˆ๊ฒ ์ฃ ?

   extension AddViewController:UISearchResultsUpdating {
       func filteredContentForSearchText(_ searchText:String){
           filteredLocation = allLocations.filter({ (location) -> Bool in
               return location.city.lowercased().contains(searchText.lowercased()) || location.country.lowercased().contains(searchText.lowercased())
           })
           tableView.reloadData()
       }
       
       func updateSearchResults(for searchController: UISearchController) {
           filteredContentForSearchText(searchController.searchBar.text ?? "")
       }
   }

๋งˆ์ง€๋ง‰์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ €๋ฒˆ ์‹œ๊ฐ„์— ๋ฐฐ์šด loadLocationFromCSV ํ•จ์ˆ˜์™€ setUpSearchController ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ๋ฉด

       override func viewDidLoad() {
           super.viewDidLoad()
     
           loadLocationsFromCSV()
           setUpSearchController()
         
       }

์•„๋ž˜์™€ ๊ฐ™์ด ๋„์‹œ ์ด๋ฆ„์ด๋‚˜ ๋‚˜๋ผ ์ด๋ฆ„์„ ๊ฒ€์ƒ‰ํ–ˆ์„๋•Œ ํ•„ํ„ฐ๋ง๋œ ๊ฐ์ฒด๋งŒ ๋„์›Œ์ฃผ๋Š” ํ…Œ์ด๋ธ”๋ทฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ๊ฒ€์ƒ‰ํ•˜๊ณ  ํ•„ํ„ฐ๋งํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ์ ์ด ์žˆ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ์‚ฌํ•ญ์ด ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€