在viewController中添加地点的评论

时间:2018-02-08 14:53:51

标签: ios json swift google-places-api swift4

我想在我的viewController中添加一个地方的评论(我正在使用google places API),类似这样enter image description here

并按照谷歌地方文档(https://developers.google.com/places/web-service/details),我创建了一个viewController,里面只有一个标签和这个代码(现在我试图只显示文本和作者名称)但是不起作用(而且,我不知道标签是否适合做这种事情),有什么不对,我该如何调整呢?

// UPDATE

//的ViewController

import UIKit
import SwiftyJSON
import GooglePlaces
import Alamofire

private let RIdentifier = "myReviewTableCell"


class ReviewClass: UIViewController, UITableViewDelegate, UITableViewDataSource {



    @IBOutlet weak var RtableView: UITableView!
    @IBOutlet weak var myReview: UILabel!

    var places:[QPlace] = []
    var index2:Int = -1






    override func viewDidLoad() {
        super.viewDidLoad()

        guard index2 >= 0, places.count > 0 else {
            return
        }
        let place = places[index2]

        myReview.text = place.name

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    // TABLE VIEW





    func numberOfSections(in tableView: UITableView) -> Int {

        print("numberOfsection Call")
        return 1
    }




    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       return 1
    }




    public  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: RIdentifier, for: indexPath) as! myReviewTableCell

        let place = places[index2]

        cell.revSelect(place: place)

       return (cell)
    }

 }

//自定义TableViewCell

import UIKit

private let json = """
{
"reviews" : [
{
"author_name" : "Robert Ardill",
"author_url" : "https://www.google.com/maps/contrib/106422854611155436041/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-T47KxWuAoJU/AAAAAAAAAAI/AAAAAAAAAZo/BDmyI12BZAs/s128-c0x00000000-cc-rp-mo-ba1/photo.jpg",
"rating" : 5,
"relative_time_description" : "a month ago",
"text" : "Awesome offices. Great facilities, location and views. Staff are great hosts",
"time" : 1491144016
}
]
}
"""

class myReviewTableCell: UITableViewCell {


    @IBOutlet weak var revText: UITextView!
    @IBOutlet weak var revRating: UIImageView!
    @IBOutlet weak var revAuthor: UILabel!

     var place: QPlace?




    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }




    func revSelect(place:QPlace) {



        NearbyPlaces.getPlaceDetails(place: place) { (place) in
            DispatchQueue.main.async {
                if let review = place.details?[json] as? [String:Any] {
                    do {
                        if let data = json.data(using: .utf8) {
                            let decoder = JSONDecoder()
                            let decoded = try decoder.decode(Response.self, from: data)
                            print(decoded.reviews)
                 // revText ??
                 // revAuthor ??
                        } else {
                            print("data is not available")
                        }
                    } catch (let e) {
                        print(e)
                    }
                }
            }
        }
    }
}

struct Response: Codable {

    struct Review: Codable, CustomStringConvertible {

        let text: String
        let authorName: String

        var description: String {
            return "Review text: \(text) authorName: \(authorName)"
        }

        enum CodingKeys: String, CodingKey {
            case text
            case authorName = "author_name"
        }
    }

    let reviews: [Review]
}

1 个答案:

答案 0 :(得分:0)

上面显示的代码有很多错误,会导致一些问题。以下是一些帮助您的笔记/提示。

  1. 您需要设置tableView委托和数据源(通常为自己)
  2. 您对tableView的数据应该是当前地点的评论数组,例如var reviews = [Review]()
  3. 您不需要let place = places[index2]myReview.text = place.name
  4. 您的numberOfRowsInSection func应该返回self.reviews.count
  5. 你的tableViewCell不应该发出API请求来获取它的数据。
  6. 在视图控制器中发出您的API请求。解析响应,然后从API设置self.reviews =您的结果数据。
  7. 您的cellForRowAt func应该将单元格出列,从reviews数组中获取评论信息并将数据分配给单元格。
  8. 的示例cellForRow
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: RIdentifier, for: indexPath) as! myReviewTableCell
    
        let review = reviews[indexPath.row]
        cell.authorNameLabel.text = review.authorName
        return cell
    }
    

    当您加载此视图控制器时,您应该传递/设置该位置(您可以转到此视图控制器,然后在prepareForSegue中设置该位置),然后一旦您向API发出请求并获取所有评论地点。这是tableView的数据。

    如果我这样做,我的方法看起来就像这样......

    class ReviewsTableViewController: UIViewController {
    
        @IBOutlet weak var tableView: UITableView!
    
        var place: Place?
        var reviews = [Review]()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            guard let place = self.place else { return }
    
            tableView.dataSource = self
            tableView.delegate = self
    
            // not exact syntax, doing this from memory
            Alamofire.request(urlForApiRequest).responseJSON {
               // parse the json response to get review models using JSONDecoder()
               self.reviews = reviews
               self.tableView.reloadData()
            }
        }
    }
    

    然后,为了澄清,我将在下面为每个代表提供扩展

    extension ReviewsTableViewController: UITableViewDataSource, UITableViewDelegate {
       // table view functions in here
    }