[swift] DZNEmptyDataSet의 커스텀화 방법

DZNEmptyDataSet은 UITableView나 UICollectionView와 같은 뷰에서 아이템이 없을 때 표시되는 빈 상태를 커스텀화하는데 사용되는 라이브러리입니다. 이 라이브러리를 사용하면 더 나은 사용자 경험을 제공하고 앱의 빈 상태를 시각적으로 개선할 수 있습니다. 이번 글에서는 DZNEmptyDataSet을 사용하여 빈 상태의 뷰를 커스텀하는 방법에 대해 알아보겠습니다.

DZNEmptyDataSet 설정하기

  1. 프로젝트에 DZNEmptyDataSet 라이브러리를 추가합니다. CocoaPods를 사용하는 경우 pod 'DZNEmptyDataSet'을 Podfile에 추가하고 pod install을 실행해주세요.

  2. 사용할 뷰 컨트롤러에서 DZNEmptyDataSetDelegate와 DZNEmptyDataSetSource 프로토콜을 채택합니다.

import DZNEmptyDataSet

class ViewController: UIViewController, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource {
    // ...
}
  1. 위에서 채택한 프로토콜 메서드를 구현합니다.
func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.boldSystemFont(ofSize: 18),
        .foregroundColor: UIColor.darkGray
    ]
    return NSAttributedString(string: "빈 상태", attributes: attributes)
}

func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.systemFont(ofSize: 14),
        .foregroundColor: UIColor.lightGray
    ]
    return NSAttributedString(string: "데이터가 없습니다.", attributes: attributes)
}

func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
    return UIImage(named: "empty_image")
}

func buttonImage(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> UIImage? {
    return UIImage(named: "refresh_button")
}

func emptyDataSet(_ scrollView: UIScrollView, didTapButton button: UIButton) {
    // 버튼을 클릭했을 때 실행할 동작을 구현합니다.
}

커스텀화하기

DZNEmptyDataSet을 사용하면 다양한 방법으로 빈 상태 뷰를 커스텀할 수 있습니다. 위의 예제에서는 제목과 설명에 사용될 폰트와 색상을 설정하였고, 이미지와 버튼도 추가하였습니다. 필요한 경우 적절한 메서드를 구현하여 원하는 커스텀화를 할 수 있습니다.

참고 자료

위에서 소개된 방법을 통해 DZNEmptyDataSet을 사용하여 빈 상태의 뷰를 간단하게 커스텀할 수 있습니다. 빈 상태 뷰를 통해 사용자에게 더 나은 경험을 제공하고, 앱의 사용성을 개선할 수 있습니다.