스위프트 Table View

UITableView

override func viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()
    let tableView: UITableView = UITableView(frame: view.bounds, style: .plain)
    tableView.dataSource = self // UITableViewDataSource 선언
    tableView.delegate = self // UITableViewDelegate 선언
    tableView.register(menuCell.self, forCellReuseIdentifier: "menuCell")
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell") // tableView에 Cell Class를 Register
    view.addSubview(tableView)
}

01. numberOfRowsInSection

let menu: [String] = ["1", "2", "3", "4", "5"]

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

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

02. cellForRowAt indexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        cell.textLabel?.text = "〰️👋🏻 \(menu[indexPath.row])"
        return cell
    }else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! menuCell
        cell.setImageName(name: menu[indexPath.row])
        return cell
    }
}    

03. heightForRowAt indexPath

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // let cell: menuCell = tableView.cellForRow(at: indexPath) as! menuCell
    return 250
}

04. didSelectRowAt indexPath

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let name: String = menu[indexPath.row]
    let alert = UIAlertController(title: "확인🙃👀", message: "\(name)가 선택되었습니다.", preferredStyle: .alert)
    let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
    alert.addAction(okAction)
    present(alert, animated: true, completion: nil)
}

05. titleForHeaderInSection

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    <#code#>
}

06. canEditRowAt

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    <#code#>
}

07. scrollViewDidScroll

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if contentSize.offset.y < 100 {
        data 추가 요청
    }
}