簡體   English   中英

錯誤:類型“ UserAccView”不符合協議“ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM