【问题标题】:How do I get the value from the model to the controller如何从模型中获取值到控制器
【发布时间】:2016-04-15 14:21:45
【问题描述】:

这是我第一个使用 MVC 设计模式的程序,我不知道如何从模型中获取值到我的控制器并在我的视图中显示它。我会告诉你我做了什么。请澄清我做错了什么?或者告诉我如何以其他方式完成。

型号

class songData: NSObject {

var artistName: String
var albumName: String

init(artistName: String, albumName: String) {
    self.artistName = artistName
    self.albumName = albumName
}

}

控制器

  @IBAction func doTheSearch(sender: AnyObject) {

    itunesAPI().itunesSearch({(song : songData) in


    })

    self.tableView.reloadData()

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return song1.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

      var artistAndAlbum = itunesAPI().array[indexPath.row]

    cell.textLabel?.text =
    cell.detailTextLabel?.text =

    return cell

 }

API

  func itunesSearch(completionHandler:(songData)->()) {

    Alamofire.request(.GET, "http://itunes.apple.com/search?", parameters: ["term" : "tamil new songs", "media" : "music"])

        .responseJSON { (response) in

            let json = JSON(response.result.value!)



            if let jsonData = json["results"].arrayObject {
                self.array = jsonData as! [[String : AnyObject]]


            if self.array.count > 0 {
//                    self.array = jsonData as! [[String : AnyObject]]
//                    if let resultsDict = resultsArray.first {


                let albumName = json["results"]["collectionName"].stringValue
                let artistName = json["results"]["artistName"].stringValue


                let song = songData(artistName: artistName, albumName: albumName)

                completionHandler(song)


        }

      }

我的视图中确实没有任何内容,除了故事板,它由一个带有单个单元格的表格视图组成。我需要从 API 获取响应并将其显示在视图中。

【问题讨论】:

    标签: swift api model-view-controller alamofire


    【解决方案1】:

    首先,您需要在数据返回后重新加载表。将您的 IBAction 更新为:

    itunesAPI().itunesSearch({(song : songData) in
       self.tableView.reloadData()
    })
    

    否则 reloadData 将在数据返回之前被调用。在 viewController 上设置一个属性来存放数据。此外,最好以大写字母开头类名。

    var tableData:[SongData] = [SongData]()
    

    然后在数据成功返回时设置这个变量:

    itunesAPI().itunesSearch({(song : songData) in
       self.tableData.append(song) // add the result to the list of data
       self.tableView.reloadData() // reload the table
    })
    

    然后将单元格设置为:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    
        var artistAndAlbum = self.tableData[indexPath.row]
    
        cell.textLabel?.text = artistAndAlbum.artistName
        cell.detailTextLabel?.text = artistAndAlbum.albumName
    
        return cell
    
    }
    

    【讨论】:

    • 通过 json 值对下标稍作更改,我得到了结果。谢谢你的帮助伙计!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 2016-07-26
    • 2016-02-12
    • 2014-04-07
    • 2016-12-09
    相关资源
    最近更新 更多