StoryCode

동적으로 NavigationBar 와 TableView 추가.code

iOS/XCode Swift
반응형

//

//  ViewController.swift

//  ToDoApp2

//

//  Created by GANG WOOK KIM on 17/05/2018.

//  Copyright © 2018 GANG WOOK KIM. All rights reserved.

//


import UIKit


extension ViewController:UITableViewDataSource

{

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

        return self.items.count

    }

    

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

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier:"Cell")

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

        return cell

    }


    // 왼쪽 공백 제거

    /*

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

     

        if(self.tableView.respondsToSelector(Selector("setSeparatorInset:"))) {

            self.tableView.separatorInset = UIEdgeInsetsZero

        }

     

        if(self.tableView.respondsToSelector(Selector("setLayoutMargins:"))) {

            self.tableView.layoutMargins = UIEdgeInsetsZero

        }

     

        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {

            cell.layoutMargins = UIEdgeInsetsZero

        }

    }

    */

    

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

}


extension ViewController:UITableViewDelegate

{

    // 터치시 터치한 아이템을 처리한다.

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

        print(items[indexPath.row])

    }

}


class ViewController: UIViewController {

    // ** TableView Variable

    var items:[String] = ["First", "Second", "Third"]

    var tableView: UITableView!


   

    override func viewDidLoad() {

        super.viewDidLoad()

        let navigationBarHeight:Int = 44

        // ** Add Navigation Bar

        let navigationBar = UINavigationBar(frame:CGRect(x:0, y:0, width:Int(view.frame.size.width),height:navigationBarHeight))

        navigationBar.backgroundColor = UIColor.gray

        

        let navigationItem = UINavigationItem()

        navigationItem.title = "AlimPang"

        

        let leftButton = UIBarButtonItem(title:"Save", style: .plain, target: self, action:#selector(ViewController.btn_clicked(_:)))

        let rightButton = UIBarButtonItem(title:"Right", style: .plain, target: self, action:nil)

        

        navigationItem.leftBarButtonItem = leftButton

        navigationItem.rightBarButtonItem = rightButton

        

        navigationBar.items = [navigationItem]

        self.view.addSubview(navigationBar)

        

        // ** TableView Adding

        let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height

        let displayWidth: CGFloat = self.view.frame.width

        let displayHeight: CGFloat = self.view.frame.height

        

        tableView = UITableView(frame: CGRect(x: CGFloat(0), y: CGFloat(navigationBarHeight), width: displayWidth, height: displayHeight - barHeight - CGFloat(navigationBarHeight)))

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")

        tableView.dataSource = self

        tableView.delegate = self

        self.view.addSubview(tableView)

        

        /*

        self.tableView.translatesAutoresizingMaskIntoConstraints = false

        self.view.addConstraint(NSLayoutConstraint(item: self.tableView,

                                                   attribute: .top, relatedBy: .equal, toItem: self.view,

                                                   attribute: .top, multiplier: 1.0, constant: 0))

        self.view.addConstraint(NSLayoutConstraint(item: self.tableView,

                                                   attribute: .bottom, relatedBy: .equal, toItem: self.view,

                                                   attribute: .bottom, multiplier: 1.0, constant: 0))

        self.view.addConstraint(NSLayoutConstraint(item: self.tableView,

                                                   attribute: .leading, relatedBy: .equal, toItem: self.view,

                                                   attribute: .leading, multiplier: 1.0, constant: 0))

        */

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }


    override func viewWillAppear(_ animated: Bool)

    {

        super.viewWillAppear(animated)

        UIApplication.shared.isStatusBarHidden = true

        //tableView.reloadData()

        /*

        let screenSize:CGRect = UIScreen.main.bounds

        

        tableView.frame = CGRect(x:0, y:0, width:screenSize.width, height:screenSize.height)

        tableView.dataSource = self

        tableView.delegate = self

        tableView.register(UITableViewCell.self, forCellReuseIdentifier:"myCell")

        self.view.addSubview(tableView)

        */

    }

    @objc func btn_clicked(_ sender:UIBarButtonItem)

    {

        

    }

}


반응형

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

Android 와 SWIFT 개념 비교  (0) 2018.05.20
화면 처리 순서  (0) 2018.05.19
Status Bar 숨기기 (Carrier/ Wifi/ BatteryIcon Bar)  (0) 2018.05.18
Constraint 동적 추가  (0) 2018.05.17
CommonLib.swift  (0) 2018.05.17