json - 快速创建单元格默认值表视图单元格
问题描述
func downloadJSON(completed: @escaping () -> ()) {
let url = URL(string: "my_URL_FILE")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil {
do {
self.notifications = try JSONDecoder().decode([NotificationStats].self, from: data!)
print(self.notifications)
DispatchQueue.main.async {
completed()
}
}catch {
print("JSON Error")
}
}
}.resume()
}
如果 JSON id 为空,我需要在我的结构中显示一些内容:
struct NotificationStats:Decodable {
let Tipo: String
let Destinatario: String
let MittenteNome: String
let MittenteCognome: String
let DataOra: String
let FotoBici: String
let CoordinataX: String
let CoordinataY: String
let Stato: String
let IdNotifica: String
let IdMittente: String
let IdDestinatario: String
let IdBici: String
let FotoMittente: String
}
我正在通过这个调用导入我的结构:
var notifications = [NotificationStats]()
然后我需要在我的表格视图中创建一个单元格,例如“无通知”。
我可以给我的结构一个默认值吗?
这是我创建表格视图的代码
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
print(notifications.count)
return notifications.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! NotificheCustomTableViewCell
let imgURL = NSURL(string: "my_url/\(notifications[indexPath.row].FotoBici)")
if imgURL != nil{
let data = NSData(contentsOf: (imgURL as URL?)!)
cell.bikeImage?.image = UIImage(data: data! as Data)
}
cell.tipoLabel?.text = "\(notifications[indexPath.row].Tipo.capitalized)"
cell.dataOraLabel?.text = "\(notifications[indexPath.row].DataOra)"
let image = UIImage(named: "arrow-full.png")?.renderResizedImage(newWidth: 30)
let imageViewNew = UIImageView(image: image)
let imageSeen = UIImage(named: "arrow-empty.png")?.renderResizedImage(newWidth: 30)
let imageViewSeen = UIImageView(image: imageSeen)
if (notifications[indexPath.row].Stato == "nuova"){
cell.accessoryView = imageViewNew
}else{
cell.accessoryView = imageViewSeen
}
cell.cellView.layer.cornerRadius = 10
return cell
}
我想了解如何处理现在打印“jsonerror”的捕获事实。我想创建一个显示文本规则的单元格,以防程序输入捕获“没有通知您”。
解决方案
喜欢:
func tableView(tableView: UITableView, footerForHeaderInSection section: Int) -> CGFloat {
if notifications.count > 0 {
return 0
} else {
return hightOfView
}
}
func tableView(tableView: UITableView, viewForfooterInSection section: Int) -> UIView?{
MakeViewForDisplayNoNotification()
}
推荐阅读
- python - 处方数据集的 Python 分析 - 循环提前终止,并且没有输出文件
- javascript - 如何记录 HTML 上两个元素的更改?
- python - 熊猫组内的渐进式价值收集
- php - 我正在尝试使用 mysqli 和 php 将产品添加到数据库中,但没有添加任何内容
- java - Sending Multipart request with Jersey Client
- r - R: apply the pclm function
- java - sortWith 对某些字符串使用基本字符比较失败
- php - whoami 返回 apache 而不是 root
- jquery - 按下按钮时动画跨度
- java - android.app.ContextImpl.startServiceCommon 处的 java.lang.IllegalStateException