iOS 코드로 UITableView 구현해보기

|

개인공부 후 자료를 남기기 위한 목적임으로 내용 상에 오류가 있을 수 있습니다.


코드로 UITableView 구현

개인적으로 스토리보드를 애용해 코드로 UI를 짜본적은 없었는데, 회사에서는 UI를 모두 코드로 진행하고 있었다.
그중 좀 흥미로웠던 부분이 있어 정리해보려고 합니당.

코드로 테이블뷰를 만드는것에 있어서 저는 기본 뷰컨트롤러에 테이블뷰를 추가하고, 테이블 뷰 셀을 가져오는 형식으로 구현할 것입니다.

private let tableView: UITableView = {
   let tableView = UITableView(frame: .zero, style: .grouped)
   tableView.register(PracticeChatTableViewCell.self, forCellReuseIdentifier: PracticeChatTableViewCell.identifier)
   tableView.separatorStyle = .none
   tableView.rowHeight = UITableView.automaticDimension
   tableView.estimatedRowHeight = 150
   return tableView
}()

뷰 컨트롤러에 변수로 테이블뷰를 만드는 방식이다.
이렇게 변수로 만들어놓은 테이블뷰를 화면에 위치 시켜야겠죠? 저는 snapKit을 사용했습니다.

func initViews() {
    super.initViews()

    self.view.addSubview(self.tableView)
    self.tableView.make { (make) in
        make.edges.equalToSuperview()
    }

    self.tableView.delegate = self
    self.tableView.dataSource = self
}

이렇게 만든 initView를 viewDidLoad에서 호출해주는 것 입니다.
그리고 만들어놓은 테이블뷰 변수는 addSubview를 통해 superView와 같은 크기로 위치시켜줍니다.

그리고 웨에 적인 PracticeChatTableViewCell 파일을 만들어줍니다.

import UIKit

class PracticeChatTableViewCell: UITableViewCell {
    static let identifier = "PracticeChatCell"

    private let containerView: UIView = {
        let containerView = UIView()
        containerView.backgroundColor = .red
        return containerView
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.contentView.addSubview(self.containerView)
        self.containerView.make { (make) in
            make.edges.equalToSuperview()
        }
    }

    required init?(coder: NSCoder) {
        fatalError("")
    }
}

여기서 가장 흥미로웠던 점은 셀을 만들때 init을 생성해줘야한다는 부분이었다.

일반적으로 셀 파일에서는 앞서 뷰컨트롤러에서 했듯이 initView 함수를 만들어놓고 각 객체들의 설정들을 다뤄줬었는데(스토리보드 사용시) 코드로 이를 진행할 때에는 반드시 init을 생성해줘야한다. 그 이유는 인터페이스 빌더에서는 자동으로 이 객체들을 초기화 해주지만, 코드에서는 인터페이스 빌더를 사용하는 것이 아니기 때문에 직접 초기화를 해줘야 하는 것이다. 이렇게 초기화를 해주지 않으면 아무것도 뜨지 않는다.

실제로 아무생각없이 init을 안해줬더니 정말 아무것도 뜨지 않았다..!

뷰 컨트롤러의 전체 코드는 아래와 같다.

import UIKit

final class PracticeViewController: UIViewController {
    static func instance() -> PracticeViewController? {
        return PracticeViewController()
    }

    private let tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.register(PracticeChatTableViewCell.self, forCellReuseIdentifier: PracticeChatTableViewCell.identifier)
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 150
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        initViews()
    }

    func initViews() {
        super.initViews()

        self.view.addSubview(self.tableView)
        self.tableView.make { (make) in
            make.edges.equalToSuperview()
        }

        self.tableView.delegate = self
        self.tableView.dataSource = self
    }
}

extension PracticeViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: PracticeChatTableViewCell.identifier, for: indexPath)
        return cell
    }
}

Xcode Storyboard 삭제 및 Scene delegate 삭제하는 방법

|

개인공부 후 자료를 남기기 위한 목적임으로 내용 상에 오류가 있을 수 있습니다.


회사에서는 모든 UI를 코드로 짜고있다. 스토리보드를 무척 사랑했던 나로서는 정말 슬픈일이었지만, 어쩌겠나요!
앞으로는 코드로 UI를 짜는것이 습관화 되어야 하기에 개인 실습 프로젝트를 하나 파게 되었고
그러면서 XCode에서 스토리보드와 Scene delegate를 샂게하는 방법을 같이 정리해보려고 합니다!

Storyboard 삭제하기

스토리보드를 제거하기 위해서는 프로젝트가 생성되는 Main.Storyboard와의 연동된 부분을 끊으면 된다.

Info.plist에서 Main Storyboard file base name > Main을 지운다.

혹은 프로젝트 설정에서 [Main Interface]에서 Main을 지우면 Info.plist에도 반영된다. 이 방법을 사용해도 같이 반영된다.

Main.Storyboard 파일을 삭제한다.

해당 파일은 더이상 사용하지 않기 때문에 삭제해도 괜찮다.

SceneDelegate 삭제하기

기존에 SceneDelegate에서 UIWindow를 설정하는 부분을 예전처럼 AppDelegate로 옮기고, Scene관련 파일과 설정을 제거한다.

AppDelegate 에서 Scene 관련 함수 정의부를 제거한다.

// MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

AppDelegate에 UIWindow 설정 로직을 추가한다.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow()
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()

        return true
    }    
}

SceneDelegate 파일을 삭제한다.

해당 파일은 더이상 사용하지 않기에 삭제하도록 한다.

Info.plist에서 Application Scene Manifest 항목을 통쨰로 삭제한다.

확인해보기

ViewController의 기본뷰에 배경색을 입히고 앱을 실행시켜 적용한 배경색이 잘 뜨는지 확인해보자.

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        view.backgroundColor = .blue
    }
}

자주 사용하지만 헷갈리는 git 명령어 정리해보기

|

개인적인 연습 내용을 정리한 글입니다.
더 좋은 방법이 있거나, 잘못된 부분이 있으면 편하게 의견 주세요. :)


git branch 생성하기

git branch [만들 브랜치 이름]

git branch 확인하기

git branch -r  // 원격 저장소 브랜치 리스트 확인
git branch -a  // 원격, 로컬 모든 저장소의 브랜치 리스트 확인

git branch 가져오기

git checkout -t [가져올 브랜치 경로/이름]

git branch 이름 변경하기

git branch -m [기존 브랜치 이름] [바꾸고 싶은 브랜치 이름]

iOS DZNEmptyDateSet 라이브러리 사용해보기

|

개인공부 후 자료를 남기기 위한 목적임으로 내용 상에 오류가 있을 수 있습니다.


DZNEmptyDataSet

테이블 뷰나 컬렉션 뷰 등에 데이터가 없을 때, 보여줄 수 있는 심플한 화면을 손쉽게 관리할 수 있는 라이브러리

사용이유

  1. 데이터가 없을때의 단순히 흰색 화면을 피학 화면이 비어있는 이유를 사용자에게 전달 가능
  2. 일관성 유지 및 사용자 경험 개선 제공
  3. 브랜드 존재감 제공

Features

  • UITableView, UICollectionView와 호환이 된다. 뿐만 아니라 UISearchDisplayController, UIScrollView와도 호환가능
  • 이미지, 제목 및 설명 레이블, 버튼을 표시함으로써 레이아웃 및 모양의 다양성을 제공
  • NSAttributedString 또한 사용가능
  • 오토레이아웃을 사용함으로써 회전과 함께 콘텐츠를 자동으로 뷰의 중앙에 배치시켜준다. > 수직, 수평 정렬을 허용
  • 배경색상 또한 사용자 정의 가능
  • 테이블뷰 탭 제스처 허용
  • 스토리보드와 호환 가능
  • iOS6, tcOS9 이상부터 호환가능, iPhone, iPad, Apple TV와 호환 가능

해당 라이브러리는 UITableView, UICollectionView 클래스를 확장할 필요 없는 방식으로 설계되어있다.
UITableViewController, UICollectionViewController를 사용할 때 여전히 작동이 가능하다.

DZNEmptyDataSetDelegate, DZNEmptyDataSetSource 만 준수한다면 애플리케이션의 내용과 빈 상태의 모양을 완전히 사용자 지정 가능하다.

사용해보기

pod 'DZNEmptyDateSet'

viewcontroller

class ViewController: UIViewController {
  func viewDidLoad() {
    self.viewDidLoad()

    self.tableView.emptyDataSetSource = self
    self.tableView.emptyDataSetDelegate = self
  }
}

// MARK: DZNEmptyDataSetDelegate
extension ViewController: DZNEmptyDataSetDelegate {
    // 스크롤 권한 요청 > default는 false
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true/false
    }
}

// MARK: DZNEmptyDataSetSource
extension ViewController: DZNEmptyDataSetSource {
    // 비어있는 상태의 이미지 설정
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        return
    }
    // 비어있는 상태의 제목 설정
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        return
    }
}

iOS Completion Handler 사용해보기 (return과의 차이점 알아보기)

|

개인공부 후 자료를 남기기 위한 목적임으로 내용 상에 오류가 있을 수 있습니다.