Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 创建单元格默认值表视图单元格swift_Json_Swift_Uitableview - Fatal编程技术网

Json 创建单元格默认值表视图单元格swift

Json 创建单元格默认值表视图单元格swift,json,swift,uitableview,Json,Swift,Uitableview,如果JSON id为空,我需要在我的结构中显示一些内容: func downloadJSON(completed: @escaping () -> ()) { let url = URL(string: "my_URL_FILE") URLSession.shared.dataTask(with: url!) { (data, response, error) in if error == nil { do {

如果JSON id为空,我需要在我的结构中显示一些内容:

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()
}
我正在通过此调用导入我的结构:

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]()
我想了解如何处理现在打印“jsonerror”的捕获事实。 我想创建一个显示文本规则的单元格,以防过程输入catch“no notification for your”。

是否希望:

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
    }

您可以使用扩展在tableView中显示默认视图

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()
}
将以下内容添加到numberOfRowsInSection方法中

extension UITableView {

    func setEmptyMessage(_ message: String, backgroundColor: UIColor, textColor: UIColor) {
        let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
        messageLabel.text = message
        messageLabel.textColor = textColor
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = .center
        messageLabel.font = UIFont(name: "TrebuchetMS", size: 15)
        messageLabel.sizeToFit()

        self.backgroundView = messageLabel
        self.backgroundView?.backgroundColor = backgroundColor
        self.separatorStyle = .none
    }

    func restore() {
        self.backgroundView = nil
        self.separatorStyle = .singleLine
    }
}

不相关:不要使用
NSData(contentsOf:(imgURL as URL?!)
,这是同步的,因为它是在主线程中完成的,这会阻止整个应用程序。我可以使用哪个调用作为此NSData(contentsOf:(imgURL as URL?)的替代方法呢?添加一个额外的属性或协议来了解类型(错误,无通知),在
cellForRowAt:
中,检查do`let notification:NotificationStats=notifications[indexPath.row],并切换该类型的大小写。您能准确地发布您的代码吗?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if notifications.count == 0 {
        self.yourTableView.setEmptyMessage("There is no notifications to display.", backgroundColor: UIColor.clear, textColor: UIColor.lightGray)
    } else {
        self.yourTableView.restore()
    }
    return notifications.count
}