本帖最后由 莫问刀 于 2021-3-16 09:39 编辑
先看看效果吧
空数据处理
[Java] 纯文本查看 复制代码 import UIKit
public extension UITableView{
/// Displays no data available in user friendly way
/// - Parameters:
/// - image: Image related to the data in the table
/// - title: String title that will be shown below image
func setEmptyDataView(image: UIImage, title: String){
// Create uiview to fill entire table view
let emptyView = UIView(frame: CGRect(x: self.center.x, y: self.center.y, width: self.bounds.size.width, height: self.bounds.size.height))
let picture = UIImageView()
let titleLabel = UILabel()
picture.translatesAutoresizingMaskIntoConstraints = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false
picture.image = image
picture.contentMode = .scaleAspectFit
titleLabel.text = title
if #available(iOS 13.0, *) {
titleLabel.textColor = UIColor.secondaryLabel
} else {
titleLabel.textColor = UIColor.black
}
titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
titleLabel.textAlignment = .center
emptyView.addSubview(picture)
emptyView.addSubview(titleLabel)
let constraints = [
picture.widthAnchor.constraint(equalToConstant: 80),
picture.heightAnchor.constraint(equalToConstant: 80),
picture.centerYAnchor.constraint(equalTo: emptyView.centerYAnchor),
picture.centerXAnchor.constraint(equalTo: emptyView.centerXAnchor),
titleLabel.centerXAnchor.constraint(equalTo: emptyView.centerXAnchor),
titleLabel.topAnchor.constraint(equalTo: picture.bottomAnchor, constant: 10)
]
NSLayoutConstraint.activate(constraints)
self.backgroundView = emptyView
}
/// Removes empty data view from tableView
func removeEmptyDataView(){
self.backgroundView = nil
}
}
扩展uitableview。
使用:
[Java] 纯文本查看 复制代码 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//返回数组的大小
if preferentialList.isEmpty {
tableView.separatorStyle = .none
tableView.setEmptyDataView(image: UIImage(systemName: "tortoise") ?? UIImage(), title: "推荐商品正在赶来")
}else{
tableView.separatorStyle = .singleLine
tableView.removeEmptyDataView()
}
return preferentialList.count
}
整个流程就是。
`默认显示空数据` =》`数据加载完成` =》`隐藏中间,显示列表`
整个商品的列表加载就完善了。
下拉刷新,上拉加载更多,空白数据页面。
`计划`
>打开商品的领券页面。
>生成商品的优惠券
>复制到粘贴板中
代码地址:
https://github.com/cat13954/IOSUiTableViewSample |