【发布时间】:2017-02-25 00:12:52
【问题描述】:
我有一个从 JSON 请求中检索到的字典,我想用这些值填充一个表。我认为一切都很好,但是我在 Type SubjectsViewController does not conform to protocol 'UITableViewDataSource' 使用情节提要的类声明中遇到错误,我将 UI 上的表格视图添加到 datasource 和 delegate,据我所知,我正在正确实现这些功能。我在下面添加了我的代码的相关部分。
查看this 可能很有用,这是我发布的一个问题,以了解我为什么使用回调。提前致谢!
class SubjectsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
getSubjects(callback: {(resultValue)-> Void in
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return resultValue.count
}
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = resultValue[indexPath.row] as! String?
return cell
}
})
}
// Initialise components of the view
let UserDetails = UserDefaults.standard.stringArray(forKey: "UserDetailsArray") ?? [String]()
@IBOutlet weak var tableView: UITableView!
// Function to retrieve the subjects for the user
func getSubjects(callback: @escaping (NSDictionary)-> Void){
let myUrl = NSURL(string: "www.mydomain.com/retrieveSubjects.php");
let request = NSMutableURLRequest(url:myUrl as! URL)
let user_id = UserDetails[0]
request.httpMethod = "POST";
let postString = "user_id=\(user_id)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil {
print("error=\(error)")
return
}
var err: NSError?
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let resultValue: NSDictionary = parseJSON["subjects"] as! NSDictionary
callback(resultValue)
}
} catch let error as NSError {
err = error
print(err!);
}
}
task.resume();
}
}
编辑:在展开可选值时意外发现 CLASS throwing nil
class SubjectsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var dataDict: [String:AnyObject]?
@IBOutlet var tableView: UITableView!
let userDetails: [String] = UserDefaults.standard.stringArray(forKey:"UserDetailsArray")!
override func viewDidLoad() {
super.viewDidLoad()
self.dataDict = [String:AnyObject]()
self.tableView.delegate = self
self.tableView.dataSource = self
self.getSubjects()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataDict!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
let array = self.dataDict?[String(indexPath.row)] as! [String]
print(array)
cell.textLabel?.text = array[0]
cell.detailTextLabel?.text = array[1]
return cell
}
func getSubjects() {
let myUrl = NSURL(string: "www.mydomain/retrieveSubjects.php");
let request = NSMutableURLRequest(url:myUrl as! URL)
let user_id = userDetails[0]
request.httpMethod = "POST";
let postString = "user_id=\(user_id)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
var err: NSError?
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let resultValue = parseJSON["subjects"] as! [String:AnyObject]
self.dataDict = resultValue
self.tableView.reloadData()
}
} catch let error as NSError {
err = error
print(err!);
}
}
task.resume();
}
}
【问题讨论】:
标签: swift uitableview nsdictionary