处理nIl?在UITableViewCell中



好吧,在解决了"自我"的问题之后,我现在很难理解为什么我不能从MysqlDB中打开结果。该应用程序连接、检索数据,然后可能会显示这些数据。问题是它无法展开。这里的代码

ViewController.swift

import UIKit
class ViewController: UIViewController, UITableViewDataSource,     UITableViewDelegate, HomeModelProtocol {
//downloadItems
func itemsDownloaded(items: NSArray) {
feedItems = items
self.listTable.reloadData()
}
//link tV
@IBOutlet weak var listTable: UITableView!
//Proprietà
var feedItems: NSArray = NSArray()
var selectList : ListModel = ListModel()

//tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return feedItems.count
}
//viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//set delegates and initialize a homeModel
self.listTable.delegate = self
self.listTable.dataSource = self
let homeModel = HomeModel ()
homeModel.delegate = self
homeModel.downloadItems()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {
let cellIdentifier : String = "BasicCell"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
//recupero food
let item : ListModel = feedItems[indexPath.row] as! ListModel
//reference alla label
myCell.textLabel!.text = item.description
return myCell
}}

ListModel.swift

import Foundation
class ListModel : NSObject {
//proprietà
var name: String?
var percentage: Int?
var other: String?
var id: Int?

override init(){
}
init(name: String, percentage: Int, other: String, id: Int){
self.name = name
self.percentage = percentage
self.other = other
self.id = id
}
//stampa oggetti
override var description: String{
return "Name: (name), Percentuale: (String(describing: percentage)), Altro: (other)"
}}

HomeModel.swift

import Foundation
protocol HomeModelProtocol: class {
func itemsDownloaded(items: NSArray)
} 
class HomeModel: NSObject, URLSessionDataDelegate{
//proprietà
weak var delegate: HomeModelProtocol!
var data = Data()
let urlPath: String = "http://www.sake-house.net/webServices.php"
func downloadItems(){
let url : URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: url) {(data, response, error) in
if error != nil{
print("downloasd fallito")
}else{
print("Dati scaricati")
parseJSON(data!)
}
}
task.resume()
}} 
func parseJSON(_ data:Data){
var jsonResult = NSArray()
do{
jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
} catch let error as NSError{
print (error)
}
var jsonElement = NSDictionary()
let listFoods = NSMutableArray()
for i in 0..<jsonResult.count {
jsonElement = jsonResult[i] as! NSDictionary
let listF = ListModel()
if let name = jsonElement["Name"] as? String, let percentage = jsonElement["Percentage"] as? Int, let other = jsonElement["other"] as? String, let id = jsonElement["id"] as? Int{
listF.name = name
listF.percentage = percentage
listF.other = other
listF.id = id
}
listFoods.add(listF)
//check listFoods
DispatchQueue.main.async(execute: { () -> Void in self.delegate.itemsDownload(items: listFoods)}) <--------HERE
}}//fine func parseJSON

问题出在这一行上

let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!

"线程1:致命错误:在展开可选值时意外发现nil">

ViewController.swift中。如果我删除这一行,它会起作用,并显示名称:nil

该行导致崩溃

let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!

由于您需要为该表注册单元格View,因此需要创建一个自定义单元格,然后使用

let myCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CellName

注册

tableView.register(CellName.self, forCellReuseIdentifier:cellIdentifier)

//或用于xib

tableView.register(UINib(nibName: "xibname", bundle: nil), forCellReuseIdentifier: cellIdentifier)

class CellName:UITableViewCell {
// outlets here 
}

所以,正如Sh_Khan所说,我已经尝试过了。我想我错过了什么,或者我做错了什么。Viecontroller.swift 内部

class CellName:UITableViewCell {
}
@IBOutlet weak var listTable: UITableView!

然后

tableView.register(CellName.self, forCellReuseIdentifier:cellIdentifier)
let myCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CellName

现在我看到了一些东西,但结果我看到了"零">

最新更新