Ios 处理零?在UITableViewCell中
好的,在解决了“自我”的问题后,我现在很难理解为什么我不能从MysqlDB中打开结果。应用程序连接、检索数据,然后可能会显示它们。问题是它无法打开。这是密码 ViewController.swiftIos 处理零?在UITableViewCell中,ios,swift,null,Ios,Swift,Null,好的,在解决了“自我”的问题后,我现在很难理解为什么我不能从MysqlDB中打开结果。应用程序连接、检索数据,然后可能会显示它们。问题是它无法打开。这是密码 ViewController.swift import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, HomeModelProtocol { //downloadItems func itemsD
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
}}
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)"
}}
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
ListModel.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
}}
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)"
}}
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
HomeModel.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
}}
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)"
}}
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
“线程1:致命错误:在展开可选值时意外发现nil”
在ViewController.swift
中。如果我删除这一行,它会工作并显示给我名称:nil这一行崩溃
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
由于需要为该tableView注册单元格,因此需要创建一个自定义单元格,然后使用
let myCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CellName
登记册
tableView.register(CellName.self, forCellReuseIdentifier:cellIdentifier)
//还是锡伯
tableView.register(UINib(nibName: "xibname", bundle: nil), forCellReuseIdentifier: cellIdentifier)
所以,我已经试过了。我想我错过了什么,或者我做错了。斯威夫特
class CellName:UITableViewCell {
}
@IBOutlet weak var listTable: UITableView!
然后
tableView.register(CellName.self, forCellReuseIdentifier:cellIdentifier)
let myCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CellName
现在我可以看到一些东西,但结果我看到了“nil”mm…我不知道如何使用它。我必须“tableView.注册”然后让迈塞尔?Xcode说我试图使用一个未解决的标识符“CellName”创建一个像上面那样的类,然后xib连接outlets,然后注册xib,但是我不能在classI之外使用“listTable”(也就是outlets),以某种方式解决了这个问题。我做得不对。还有,如果我仍然不明白如何使用CellName类以及为什么要在那里设置插座。现在我可以在我的应用程序中看到“nil”,当显示数据时,我以某种方式解决了这个问题。我做得不对。还有,如果我仍然不明白如何使用CellName类以及为什么要在那里设置插座。现在我可以在我的应用程序中看到“零”