[英]error : type “UserAccView” does not conform to protocol 'UITableViewDataSource'
So I am trying to add some data returned from a function and I can only access that data from inside that function so I ended up putting the table inside the function but after I did so I received the error above. 因此,我试图添加一些从函数返回的数据,并且只能从该函数内部访问该数据,因此最终将表放入该函数中,但是这样做之后,我收到了上面的错误。
Any ideas? 有任何想法吗?
This is my code: 这是我的代码:
import Foundation
import UIKit
class UserAccView: UIViewController , UITableViewDataSource {
@IBAction func GetUserInfo(_ sender: UIButton) {
guard let url = URL(string: "https://goollyapp.azurewebsites.net/api/v0.1/Goolly/User/218910182109") else{return}
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if let response = response {
print (response)
}
if let data = data {
let json = try? JSONSerialization.jsonObject(with: data, options: [])
guard let data_array = json as? NSArray else
{
return
}
for i in 0 ..< data_array.count
{
if let data_object = data_array[i] as? NSDictionary
{
if let Body = data_object["id"] as? String,
let InfoId = data_object["TransDate"] as? String,
let Title = data_object["Debt"] as? String,
let UserId = data_object["Crdit"] as? String,
let InfoType = data_object["Desc"] as? String
{}
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (data?.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.textLabel?.text = "cells"
return cell
}
}.resume()
}
}
Why you have added the dataSource methods inside your Api Call ? 为什么要在Api Call中添加dataSource方法? Write those methods outside of your
GetUserInfo IBAction
. 在
GetUserInfo IBAction
之外编写这些方法。
Secondly, now you want to reload the tableview. 其次,现在您要重新加载tableview。 For that create
IBOutlet for tableview
first and when response comes from the api you can reload the tableview after filling the response in your data
array. 为此,首先
IBOutlet for tableview
创建IBOutlet for tableview
并且当响应来自api时,您可以在将响应填充到data
数组中之后重新加载tableview。
Lastly don't use var cell = UITableViewCell()
like this in cellForRowAt
. 最后,不要在
cellForRowAt
使用var cell = UITableViewCell()
这样。 It will freeze your tableview . 它将冻结您的tableview。 Use it like this
这样使用
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell.
Hope it helps you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.