천원의 개발

IOS TableView 초기설정 본문

iOS&Swift🍎

IOS TableView 초기설정

천 원 2022. 3. 30. 04:16

class ViewController: UIViewController, UITableViewDataSource {

    let test = [String] = ["first", "second", "third"]
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return test.count  // table 개수를 text함수 개수로 설정
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "여기에는 프로토타입 셀 식별자이름", for: indexPath)
        cell.textLabel?.text = test[indexPath.row]  //셀 데이터를 요청하는 행 번호에 해당
        return cell
    }
}