StoryCode

TableView 샘플.ViewController.swift

iOS/XCode Swift
반응형

TableView.swift


import UIKit




class ViewController: UIViewController {

    @IBOutlet var myTableView:UITableView!

    var items: [String] = ["We", "Heart", "Swift"]


    override func viewDidLoad() {

        super.viewDidLoad()

        self.myTableView.register(UITableViewCell.self, forCellReuseIdentifier:"cell")

        self.view.addSubview(self.myTableView)

        //self.tableView.backgroundColor = UIColor.red

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

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }


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

    {

        return self.items.count

    }

    

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

    {

        let cell:UITableViewCell = self.myTableView.dequeueReusableCell(withIdentifier: "TableViewCell")! as UITableViewCell

        

        cell.textLabel?.text = self.items[indexPath.row]

        return cell

    }


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

    {

        print("You selected cell #\(indexPath.row)!")

    }

    

    func numberOfSections(in tableView:UITableView) -> Int

    {

        return 1;

    }


}

 

반응형

'iOS > XCode Swift' 카테고리의 다른 글

애플 Swift Standard Library  (0) 2018.05.16
Swift.org  (0) 2018.05.16
용어 정리  (0) 2018.05.16
Mac, XCode, Swift 버전 확인  (0) 2018.05.15
아웃렛 연결하기  (0) 2018.05.15