UITableView를 사용하려면 아래 처럼...
UITableViewDataSource 메서드 구현하여 데이터 제공UITableViewDelegate 메서드 구현let tableView = UITableView()
view.addSubview(tableView)
뷰 컨트롤러가 UITableViewDataSource와 UITableViewDelegate 프로토콜을 채택한다.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// ...
}
tableView.dataSource = self
tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellIdentifier")
tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 데이터배열.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
cell.textLabel?.text = 데이터배열[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// 셀이 선택되었을 때의 동작
tableView.deselectRow(at: indexPath, animated: true)
}
tableView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
tableView.frame = view.bounds
같은 테이블 뷰 동지로서 코드 보는 재미가 있군여 엇! 나도 이거했는데 엇! 나두 ! 엇! 엇!