Open chaneeii opened 1 year ago
λ§κ·Έλλ‘ νλ©΄μ΄ Empty ν λ νμν View λ‘ μ¬μ©μκ° μ무 컨ν μΈ λ μμλ λΉλ΄μ©μ΄ μλλΌ λΉλ΄μ© λμ μ΄λ ν λ΄μ©μ΄ μμ΄μΌ νλμ§, μ΄λ€ μν©μΈμ§ μΈμν μμλλ‘ νλλ° νμν View μ΄λ€.
μμ μ WWDC Writing for interfaces μμμ μ 리νλ©΄μ Empty View μ λν΄μ μ 리ν λ΄μ©μ μλ 첨λΆνκ² λ€!
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 }
EmptyView μ 2κ°μ§ νμ μ λλμ΄ μ£Όμμ΅λλ€.
enum EmptyViewType { case search case noresult }
self.backgroundView λ₯Ό λ§λ EmptyView λ‘ μ€μ ν΄μ€λλ€.
self.backgroundView
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 λ‘ μ€μ }
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 }
κ·ΈλΌ μμ± π
https://stackoverflow.com/questions/43772984/how-to-show-a-message-when-collection-view-is-empty https://gist.github.com/SwapnanilDhol/649bd2d4dd330bc902054e86a45114df
EmptyViewλ?
λ§κ·Έλλ‘ νλ©΄μ΄ Empty ν λ νμν View λ‘ μ¬μ©μκ° μ무 컨ν μΈ λ μμλ λΉλ΄μ©μ΄ μλλΌ λΉλ΄μ© λμ μ΄λ ν λ΄μ©μ΄ μμ΄μΌ νλμ§, μ΄λ€ μν©μΈμ§ μΈμν μμλλ‘ νλλ° νμν View μ΄λ€.
μμ μ WWDC Writing for interfaces μμμ μ 리νλ©΄μ Empty View μ λν΄μ μ 리ν λ΄μ©μ μλ 첨λΆνκ² λ€!
Typeμ΄ μλ TableViewμ EmptyView λ§λ€κΈ°
기본컨μ
1. EmptyView κ΅¬μ± ννΈ
TableViewμ extension μ λ§λ€κ³ , 1οΈβ£ EmptyView μΌλ μ€μ νλ ν¨μμ 2οΈβ£ μλλλ₯Ό μν΄ λ€μ λλ €λλ ν¨μλ₯Ό λ§λλλ€.
2. EmptyView μ μ© ννΈ
κ·Έλ¦¬κ³ UITableViewDataSource μ numberOfRowsInSectionμμ μ¬μ©ν΄μ€λλ€.
λ§λ€ EmtpyView λΆμνκΈ°
νμ μ λλλλ€.
EmptyView μ 2κ°μ§ νμ μ λλμ΄ μ£Όμμ΅λλ€.
setEmptyView ν¨μλ₯Ό μμ±ν΄μ€λλ€.
self.backgroundView
λ₯Ό λ§λ EmptyView λ‘ μ€μ ν΄μ€λλ€.restore ν¨μλ₯Ό μμ±ν΄μ€λλ€.
μ μ©μμΌμ€λλ€! ( μν©μ λ§κ²..!)
κ·ΈλΌ μμ± π
Ref
https://stackoverflow.com/questions/43772984/how-to-show-a-message-when-collection-view-is-empty https://gist.github.com/SwapnanilDhol/649bd2d4dd330bc902054e86a45114df