【问题标题】:Populating a UITable with NSDictionary in SWIFT在 SWIFT 中使用 NSDictionary 填充 UITable
【发布时间】:2017-02-25 00:12:52
【问题描述】:

我有一个从 JSON 请求中检索到的字典,我想用这些值填充一个表。我认为一切都很好,但是我在 Type SubjectsViewController does not conform to protocol 'UITableViewDataSource' 使用情节提要的类声明中遇到错误,我将 UI 上的表格视图添加到 datasourcedelegate,据我所知,我正在正确实现这些功能。我在下面添加了我的代码的相关部分。

查看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


    【解决方案1】:

    这是您需要做的:

    首先,为您的数据创建一个类变量。把它放在你的类声明下:

    var dataDict: [String:AnyObject]?
    

    然后,为了让事情井井有条,请将 tableView 属性和 userDetails 属性直接放在 dataDict 下。

    @IBOutlet var tableView: UITableView!
    let userDetails: [String] = UserDefaults.standard.stringArray(forKey:"UserDetailsArray")
    

    现在,您的 viewDidLoad 方法应该如下所示:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataDict = [String:AnyObject]()
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.getSubjects()
    }
    

    在此之后,您需要处理 UITableViewDataSource 方法:

    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")
        if let array = self.dataDict[String(indexPath.row)] as? [String] {
            cell.textLabel?.text = array[0]
            cell.detailTextLabel?.text = array[1]
        }
        return cell
    }
    

    最后,你需要实现getSubjects()方法:

    func getSubjects() {
    
        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 = parseJSON["subjects"] as! [String:AnyObject]
                    self.dataDict = resultValue
                    self.tableView.reloadData()
                }
            } catch let error as NSError {
                err = error
                print(err!);
            }
        }
        task.resume();
    }
    

    总而言之,您的代码应如下所示:

    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")
            if let array = self.dataDict[String(indexPath.row)] as? [String] {
                cell.textLabel?.text = array[0]
                cell.detailTextLabel?.text = array[1]
            }
            return cell
        }
    
        func getSubjects() {
    
            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 = parseJSON["subjects"] as! [String:AnyObject]
                        self.dataDict = resultValue
                        self.tableView.reloadData()
                    }
                } catch let error as NSError {
                    err = error
                    print(err!);
                }
            }
            task.resume();
        }
    }
    

    【讨论】:

    • 嗨 BawPotter!非常感谢您的回复并充分解释了答案!但是在tableView(cellForRowAt) 函数中,resultValue 是未解决的吗?还有,为什么大部分函数都是overrides
    • 他假设这是一个 UITableViewController。不是,因此它们不会在您自己的代码中被覆盖。
    • @Bawpotter:我用dataDict[indexPath.row] 替换了resultValue,但我收到Type [String:AnyObject]? has no subscript members 的错误
    • 能否请您复制并粘贴您的 JSON 数据?我想看看你的结构方式,这可能是问题所在。
    • 没问题!很高兴我能为您完成这项工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    相关资源
    最近更新 更多