<--! [iOS] 테이블뷰 만들기 (1) -->

[iOS] 테이블뷰 만들기 (1)

필그램

·

2017. 8. 29. 02:54


먼저 스토리 보드에 TabelView를 그림과 같이 넣는다.






[결과화면]



[소스코드] 설명은 코드내에..




import UIKit


class ViewController: UIViewController, UITableViewDataSource {

    //0. UITableViewDataSource 입력

    

    //1. 어레이 입력 - 출력될 내용

    let data:[String] = ["Item1", "Item2", "Item3"]


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }


    // 2. numberOfRowsInSection - 어레이를 카운팅 

         // return 5 라고하면 5개만 리스트 된다.


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return data.count

    }

    

    //3.cellForRowAt - 셀에 나올 내용 선택

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)


        //4.dequeueReusableCell 입력후, 스토리보드에서 "cell" 입력(맨위 이미지 참조)

        cell.textLabel?.text = data[indexPath.row]

        return cell

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}




반응형