Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UITableView EmptyView λ§Œλ“€κΈ° 🍚 #56

Open
chaneeii opened this issue Oct 26, 2022 · 0 comments
Open

UITableView EmptyView λ§Œλ“€κΈ° 🍚 #56

chaneeii opened this issue Oct 26, 2022 · 0 comments
Labels
UIKit UIKit

Comments

@chaneeii
Copy link
Owner

chaneeii commented Oct 26, 2022

EmptyViewλž€?

λ§κ·ΈλŒ€λ‘œ 화면이 Empty ν• λ•Œ ν•„μš”ν•œ View 둜 μ‚¬μš©μžκ°€ 아무 컨텐츠도 μ—†μ„λ•Œ λΉˆλ‚΄μš©μ΄ μ•„λ‹ˆλΌ λΉˆλ‚΄μš© λŒ€μ‹  μ–΄λ– ν•œ λ‚΄μš©μ΄ μžˆμ–΄μ•Ό ν–ˆλŠ”μ§€, μ–΄λ–€ 상황인지 μΈμ‹ν• μˆ˜μžˆλ„λ‘ ν•˜λŠ”λ° ν•„μš”ν•œ View 이닀.

μ˜ˆμ „μ— WWDC Writing for interfaces μ˜μƒμ„ μ •λ¦¬ν•˜λ©΄μ„œ Empty View 에 λŒ€ν•΄μ„œ μ •λ¦¬ν•œ λ‚΄μš©μ„ μ•„λž˜ μ²¨λΆ€ν•˜κ² λ‹€!

스크란샷 2022-11-01 α„‹α…©α„Œα…₯ᆫ 9 26 35

Type이 μžˆλŠ” TableView의 EmptyView λ§Œλ“€κΈ°

기본컨셉

1. EmptyView ꡬ성 파트

TableView의 extension 을 λ§Œλ“€κ³ ,
1️⃣ EmptyView μΌλ•Œ μ„€μ •ν•˜λŠ” ν•¨μˆ˜μ™€
2️⃣ μ•„λ‹λ•Œλ₯Ό μœ„ν•΄ λ‹€μ‹œ λŒλ €λ‘λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“­λ‹ˆλ‹€.

import UIKit

extension UITableView {
   func setEmptyView()  // 1️⃣
   func restore() // 2️⃣
}

2. EmptyView 적용 파트

그리고 UITableViewDataSource 의 numberOfRowsInSectionμ—μ„œ μ‚¬μš©ν•΄μ€λ‹ˆλ‹€.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  if searchResults.count == 0 {
          tableView.setEmptyView()
  } else {
      tableView.restore()
  }
  return searchResults.count
}

λ§Œλ“€ EmtpyView λΆ„μ„ν•˜κΈ°

μ œκ°€ λ§Œλ“€ EmptyView λŠ” λ‹€μŒκ³Ό 같이 2κ°€μ§€λ‘œ νƒ€μž…μ΄ λ‚˜λ‰˜μ–΄μ•Όν–ˆλŠ”λ°μš”!

κ²€μƒ‰ν•˜κΈ° μ „ 검색결과가 μ—†λŠ” 경우
스크란샷 2022-11-01 α„‹α…©α„Œα…₯ᆫ 9 29 41 스크란샷 2022-11-01 α„‹α…©α„Œα…₯ᆫ 9 29 58

νƒ€μž…μ„ λ‚˜λˆ•λ‹ˆλ‹€.

EmptyView 의 2가지 νƒ€μž…μ„ λ‚˜λˆ„μ–΄ μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

    enum EmptyViewType {
        case search
        case noresult
    }

setEmptyView ν•¨μˆ˜λ₯Ό μž‘μ„±ν•΄μ€λ‹ˆλ‹€.

self.backgroundView λ₯Ό λ§Œλ“  EmptyView 둜 μ„€μ •ν•΄μ€λ‹ˆλ‹€.

func setEmptyView(message: String, type: EmptyViewType) {
        
        // βœ… EmptyView
        let emptyView = UIView(frame: CGRect(x: self.center.x,
                                             y: self.center.y,
                                             width: self.bounds.size.width,
                                             height: self.bounds.size.height))
           
        let iconView: UIImageView = {
            $0.translatesAutoresizingMaskIntoConstraints = false
            $0.image = UIImage((type == .search) ?  .img_emptyfriends_search : .img_emptyfriends_noresult)
            $0.layer.applyFigmaShadow(
                color: .black,
                opacity: 0.1,
                xCoord: 0,
                yCoord: 0,
                blur: 5,
                spread: 0
            )
            return $0
        }(UIImageView())
        
        let messageLabel: UILabel = {
            $0.translatesAutoresizingMaskIntoConstraints = false
            $0.text = message
            $0.textColor = (type == .noresult) ? .zestyColor(.dim) : .label
            $0.numberOfLines = 0
            $0.textAlignment = .center
            $0.font = .systemFont(ofSize: 17, weight: .medium)
            $0.sizeToFit()
            return $0
        }(UILabel())
        
        self.addSubview(emptyView)
        
        emptyView.addSubviews([iconView, messageLabel])
        NSLayoutConstraint.activate([
            iconView.centerYAnchor.constraint(equalTo: emptyView.centerYAnchor, constant: -180),
            iconView.centerXAnchor.constraint(equalTo: emptyView.centerXAnchor),
            iconView.widthAnchor.constraint(equalToConstant: 50),
            iconView.heightAnchor.constraint(equalToConstant: 50),
            messageLabel.topAnchor.constraint(equalTo: iconView.bottomAnchor, constant: 10),
            messageLabel.leadingAnchor.constraint(equalTo: emptyView.leadingAnchor, constant: 20),
            messageLabel.trailingAnchor.constraint(equalTo: emptyView.trailingAnchor, constant: -20)
        ])
        
        self.backgroundView = emptyView // βœ… EmptyView λ₯Ό background 둜 μ„€μ •
    }

restore ν•¨μˆ˜λ₯Ό μž‘μ„±ν•΄μ€λ‹ˆλ‹€.

    func restore() {
        self.backgroundView = nil
    }

μ μš©μ‹œμΌœμ€λ‹ˆλ‹€! ( 상황에 맞게..!)

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchResults.count == 0 {
            if searchingTextFieldView.textField.text == "" {
                tableView.setEmptyView(message: "λ“±λ‘ν•˜λ €λŠ” 맛집을\nκ²€μƒ‰ν•΄μ£Όμ„Έμš”.", type: .search)
            } else {
                tableView.setEmptyView(message: "검색 κ²°κ³Όκ°€ μ—†μ–΄μš”.", type: .noresult)
                
            }
        } else {
            tableView.restore()
        }
        return searchResults.count
    }

그럼 μ™„μ„± πŸš€

Ref

https://stackoverflow.com/questions/43772984/how-to-show-a-message-when-collection-view-is-empty
https://gist.github.com/SwapnanilDhol/649bd2d4dd330bc902054e86a45114df

@chaneeii chaneeii added the UIKit UIKit label Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
UIKit UIKit
Projects
None yet
Development

No branches or pull requests

1 participant