[英]Getting error in Xcode type “ViewController” does not conform to protocol“UITableViewDataSource”
[英]error : type “UserAccView” does not conform to protocol 'UITableViewDataSource'
因此,我試圖添加一些從函數返回的數據,並且只能從該函數內部訪問該數據,因此最終將表放入該函數中,但是這樣做之后,我收到了上面的錯誤。
有任何想法嗎?
這是我的代碼:
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()
}
}
為什么要在Api Call中添加dataSource方法? 在GetUserInfo IBAction
之外編寫這些方法。
其次,現在您要重新加載tableview。 為此,首先IBOutlet for tableview
創建IBOutlet for tableview
並且當響應來自api時,您可以在將響應填充到data
數組中之后重新加載tableview。
最后,不要在cellForRowAt
使用var cell = UITableViewCell()
這樣。 它將凍結您的tableview。 這樣使用
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell.
希望對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.