Skip to content

검색바  #15

Description

@yoona607

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet var table: UITableView!
@IBOutlet var searchBar: UISearchBar!

var gameArray = [Game]() // 아래에있는 게임 어레이 임시로 읽음
var currentGameArray = [Game]() //update table
 
override func viewDidLoad() {
    super.viewDidLoad()
    setUpGames()
    setUpSearchBar()
    alterLayout()
}
 
private func setUpGames() { //게임추가
    // CATS
    gameArray.append(Game(name: "Amber", category: .cat, image:"lol"))
    gameArray.append(Game(name: "James", category: .cat, image:"mainteaser"))
    gameArray.append(Game(name: "Peter", category: .cat, image:"mqdefault"))
    // DOGS
    gameArray.append(Game(name: "Haywood", category: .dog, image:"Sample1"))
    gameArray.append(Game(name: "Shell", category: .dog, image:"Sample2"))
    gameArray.append(Game(name: "James", category: .dog, image:"Sample3"))
     
    currentGameArray = gameArray
}
 
private func setUpSearchBar() { ///////////서치바 델리게이트함
    searchBar.delegate = self
}
 
func alterLayout() {
    table.tableHeaderView = UIView()
    // search bar in section header
    table.estimatedSectionHeaderHeight = 50
    // search bar in navigation bar
    //navigationItem.leftBarButtonItem = UIBarButtonItem(customView: searchBar)
    navigationItem.titleView = searchBar
    searchBar.showsScopeBar = false // you can show/hide this dependant on your layout
    searchBar.placeholder = "Search Game by Name"
}
 
// Table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return currentGameArray.count //게임 수만큼의 테이블 생성
}
 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TableCell else {
        return UITableViewCell() //셀 정의 함
    }
    cell.nameLbl.text = currentGameArray[indexPath.row].name
    //cell.categoryLbl.text = currentGameArray[indexPath.row].category.rawValue
    cell.imgView.image = UIImage(named:currentGameArray[indexPath.row].image)
    return cell
}
 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 200 //테이블 높이
}
 
// This two functions can be used if you want to show the search bar in the section header
//    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        return searchBar
//    }
 
//    // search bar in section header
//    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        return UITableViewAutomaticDimension
//    }
 
// Search Bar
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    currentGameArray = gameArray.filter({ game -> Bool in
        switch searchBar.selectedScopeButtonIndex {
        case 0:
            if searchText.isEmpty { return true } /////////////////검색하는 내용
            return game.name.lowercased().contains(searchText.lowercased())
        case 1:
            if searchText.isEmpty { return game.category == .dog }
            return game.name.lowercased().contains(searchText.lowercased()) &&
                game.category == .dog
        case 2:
            if searchText.isEmpty { return game.category == .cat }
            return game.name.lowercased().contains(searchText.lowercased()) &&
                game.category == .cat
        default:
            return false
        }
    })
    table.reloadData()
}
 
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    switch selectedScope {
    case 0:
        currentGameArray = gameArray
    case 1:
        currentGameArray = gameArray.filter({ game -> Bool in
            game.category == GameType.dog
        })
    case 2:
        currentGameArray = gameArray.filter({ game -> Bool in
            game.category == GameType.cat
        })
    default:
        break
    }
    table.reloadData()
}

}

class Game {
let name: String
let image: String
let category: GameType

init(name: String, category: GameType, image: String) {
    self.name = name
    self.category = category
    self.image = image
}

}

enum GameType: String {
case cat = "Cat"
case dog = "Dog"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions