swift tableview 간단한 스위프트 테이블뷰 , 디테일 뷰 



//

//  ViewController.swift

//  SimpleTable

//

//  Created by stayfoolish on 15/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController {

    

    @IBOutlet var tableView : UITableView!

    let dataSource: TableData = TableData()

    let delegate: TableDelegate = TableDelegate()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        self.tableView.dataSource = self.dataSource

        self.tableView.delegate = self.delegate

        

    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        guard let cell: UITableViewCell = sender as? UITableViewCell else {

            return

        }

        

        guard let index: IndexPath = self.tableView.indexPath(for: cell) else {

            return

        }

        

        guard let nextViewController: DetailViewController = segue.destination as? DetailViewController else {

            return

        }

        

        nextViewController.index = index

    }


}



//

//  TableData.swift

//  SimpleTable

//

//  Created by stayfoolish on 15/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class TableData: NSObject, UITableViewDataSource {

    

    // 섹션수

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

        return 3

    }

    

    // 섹션별 로우수

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

        

        if section == 0 {

            return 1

        }

        return 15

    }

    

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

        let cell: UITableViewCell

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

        cell.textLabel?.text = "\(indexPath.section) - \(indexPath.row)"

        

        return cell

    }


}


//

//  TableDelegate.swift

//  SimpleTable

//

//  Created by stayfoolish on 15/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class TableDelegate: NSObject, UITableViewDelegate {

    

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

        print("did select \(indexPath)")

    }


}



//

//  DetailViewController.swift

//  SimpleTable

//

//  Created by stayfoolish on 15/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class DetailViewController: UIViewController {


    @IBOutlet var label: UILabel!

    var index: IndexPath!

    

    

    override func viewDidLoad() {

        super.viewDidLoad()


        // Do any additional setup after loading the view.

        self.label.text = "\(self.index.description)"

    }

    


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destination.

        // Pass the selected object to the new view controller.

    }

    */


}












+ Recent posts