본문 바로가기

Programming/iOS

TableView 관련 2

코드 상의 주석으로 기록해둠


스토리보드 상에서 identifier를 잘 넣어두고, autoLayout에 constraint를 꼭 넣어줄 것


import UIKit


class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    

    @IBOutlet weak var mainMenuTableView: UITableView!

    

    let sampleData = SampleData()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        mainMenuTableView.delegate = self

        // controller 에 tableview 뷰를 보여주게 함

        mainMenuTableView.dataSource = self

        // tableview 에 item 갯수에 맞는 줄만 보여주고 여분의 줄은 사라지게 함

        mainMenuTableView.tableFooterView = UIView(frame: .zero)

    }

    

    // MainViewController 가 보이기 시작할 때 실행

    override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)

        // navigation bar 가 large 크기가 됨

        self.navigationController?.navigationBar.prefersLargeTitles = true

    }

    

    // MainViewController 가 안보이기 시작할 때 실행

    override func viewWillDisappear(_ animated: Bool) {

        super.viewWillDisappear(animated)

        // navigation bar 가 작아짐

        self.navigationController?.navigationBar.prefersLargeTitles = false

    }


    // item 숫자 (android adapter 에서 list size 하고 같음)

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

        return sampleData.samples.count

    }

    

    // item 에 해당하는 view 들의 값을 넣어줌

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "mainMenuCell", for: indexPath) as! MainMenuCell

        let sample = sampleData.samples[indexPath.row]

        cell.titleLabel.text = sample.title

        cell.descLabel.text = sample.desc

        cell.imageView?.image = UIImage(named: sample.image)

        return cell

    }

    

    // tableview의 각각의 item 을 클릭했을 때

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)

        switch indexPath.row {

        case 0:

            // performSegue = startActivity (액티비티 이동, 화면이동)

            self.performSegue(withIdentifier: "photoObjectDetection", sender: nil)

        case 1:

            self.performSegue(withIdentifier: "realTimeObjectDetection", sender: nil)

        case 2:

            self.performSegue(withIdentifier: "facialAnalysis", sender: nil)

        default:

            return

        }

    }

}

'Programming > iOS' 카테고리의 다른 글

TableView 관련  (0) 2018.02.07