简体   繁体   English

错误:类型“ UserAccView”不符合协议“ UITableViewDataSource”

[英]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.

相关问题 Xcode类型“ ViewController”中出现错误不符合协议“ UITableViewDataSource” - Getting error in Xcode type “ViewController” does not conform to protocol“UITableViewDataSource” 与“类型&#39;ViewController&#39;不符合协议&#39;UITableViewDataSource&#39;”相关的编译和构建错误 - Compile & Build error related to “Type 'ViewController' does not conform to protocol 'UITableViewDataSource'” Swift错误类型&#39;usersVC&#39;不符合协议&#39;UITableViewDataSource&#39; - Swift Error type 'usersVC' does not conform to protocol 'UITableViewDataSource' 类型的viewcontroller不符合协议uitableviewdatasource - type viewcontroller does not conform to protocol uitableviewdatasource 类型TypeViewOne不符合协议UITableViewDataSource - type TypeViewOne does not conform to protocol UITableViewDataSource 类型“ ViewController”不符合协议“ UITableViewDataSource” - Type “ViewController” does not conform to protocol 'UITableViewDataSource" 类型“ViewController”不符合协议“UITableViewDataSource” - Type 'ViewController' does not conform to protocol 'UITableViewDataSource' 类型“ ViewController”不符合协议“ UITableViewDataSource” - Type'ViewController' does not conform to protocol 'UITableViewDataSource' 类型“ ViewController”不符合协议“ UITableViewDataSource” - Type 'ViewController' does not conform to protocol 'UITableViewDataSource' UITableView不符合协议UITableViewDataSource错误 - UITableView does not conform to protocol UITableViewDataSource error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM