본문 바로가기

swift/snapkit

snapkit(2)

메인 컨트롤러에 기본 메뉴를 설정한다.


기본적인 view 구성도

view->tableview


1.먼저 tableview의 snapkit 만들기

view의 tableview를 추가하고 tableview의 snapkit 정해주기

tableview의 top,bottom,left,right를 view에 맞춰준다.



import UIKit
//snapkit
extension MainController {
  func initUI() {
    self.view.addSubview(self.tableView)
    view.setNeedsUpdateConstraints()
  }

  override func updateViewConstraints() {
    if (!didSetupConstraints) {
      tableView.snp.makeConstraints { make in
        make.edges.equalToSuperview()
      }
      didSetupConstraints = true
    }

    super.updateViewConstraints()
  }
}


2.tableview의 내용을 설정한다.

tableviewcell은 기본 UITableviewCell을 사용한다.


extension MainController: UITableViewDelegate, UITableViewDataSource {
  func goViewController(vc: UIViewController) {
    self.navigationController?.pushViewController(vc, animated: true)
  }

  public func initTableView() {
    self.tableView.dataSource = self
    self.tableView.delegate = self
    self.tableView.rowHeight = 44
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: self.cellName)
  }

  public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
      -> Int {
    return menuList.count
  }

  public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
      -> UITableViewCell {
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: self.cellName)!
    cell.textLabel?.text = self.menuList[indexPath.row]

    return cell
  }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    var vc: UIViewController?
    if (indexPath.row == 0) {
      vc = View1Controller.instance()
    } else if (indexPath.row == 1) {
      vc = View2Controller.instance()
    } else if (indexPath.row == 2) {
      vc = View3Controller.instance()
    } else if (indexPath.row == 3) {
      vc = View4Controller.instance()
    } else if (indexPath.row == 4) {
      vc = View5Controller.instance()
    }
    if let _vc = vc {
      _vc.title = self.menuList[indexPath.row]
      self.goViewController(vc: _vc)
    }
  }
}



3.snapkit 함수와 tableview 함수를 호출한다.



class MainController: UIViewController {
  let cellName = "cell"
  var didSetupConstraints = false

  let menuList: [String] = ["1.View 가운데",
    "2.View 4등분",
    "3.Body,Footer 나누기",
    "4-1.TopLayoutGuide 없이",
    "4-2.TopLayoutGuide 추가"]

  let tableView: UITableView = {
    let tableView = UITableView()
    return tableView
  }()

  override func viewDidLoad() {
    super.viewDidLoad()

    self.initUI()

    self.initTableView()
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }
}



4.결과


https://github.com/choiks14/SnapkitTest


'swift > snapkit' 카테고리의 다른 글

snapkit(6)  (0) 2017.06.25
snapkit(5)  (0) 2017.06.18
snapkit(4)  (0) 2017.06.18
snapkit(3)  (0) 2017.06.18
snapkit(2)  (0) 2017.06.18
snapkit(1)  (0) 2017.06.18