简体   繁体   English

TableView 不在单元格中显示内容 (Swift)

[英]TableView does not display content in the cells (Swift)

I created a UIViewController class with a tableView to show different places in the cells, i'm working with Alamofire and Google API (maps, places), the only problem is that when i run the project the cells are empty, this is my controller class:我创建了一个带有 tableView 的 UIViewController 类来显示单元格中的不同位置,我正在使用 Alamofire 和 Google API(地图、地点),唯一的问题是当我运行项目时,单元格是空的,这是我的控制器班级:

import UIKit

class SelectClass: UIViewController, UITableViewDelegate, UITableViewDataSource  {


    @IBOutlet weak var tableView: UITableView!

    var list : [QCategoryy] = [QCategoryy]()




    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        self.title = "Categories"
        list = NearbyPlaces.getCategories()
    }



    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)


        list.sort() { $0.views > $1.views}
        tableView.reloadData()
    }


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




    @IBAction func backTapp(_ sender: Any) {

        dismiss(animated: true, completion: nil)

    }


    @IBAction func doneTapp(_ sender: Any) {



}




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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "CATEGORY_CELL"
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        cell.textLabel?.text = list[indexPath.row].name
        return cell
    }

    let nearbySearchSegueIdentifier = "goToMcourse"
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: list[indexPath.row])
    }














    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == nearbySearchSegueIdentifier {
            guard let category = sender as? QCategoryy else {
                return
            }
            if let vc = segue.destination as? CourseClass2 {
                vc.category = category
            }
        }
    }
}









extension QCategoryy {
    private static let ketPrefix = "category-"

    var views:Int {
        get {
            return UserDefaults.standard.integer(forKey: QCategoryy.ketPrefix + name)
        }
    }

    func markView() {
        UserDefaults.standard.set(views + 1, forKey: QCategoryy.ketPrefix + name)
    }
}

and these are the two classes that work with it:这些是使用它的两个类:

import Foundation
import UIKit
import CoreLocation
import Alamofire

class NearbyPlaces {
    static func getCategories() -> [QCategoryy] {
        let list:[QCategoryy] = ["Bakery", "Doctor", "School", "Taxi_stand", "Hair_care", "Restaurant", "Pharmacy", "Atm", "Gym", "Store", "Spa"]
        return list
    }

    static let searchApiHost = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
    static let googlePhotosHost = "https://maps.googleapis.com/maps/api/place/photo"

    static func getNearbyPlaces(by category:String, coordinates:CLLocationCoordinate2D, radius:Int, token: String?, completion: @escaping (QNearbyPlacesResponse?, Error?) -> Void) {

        var params : [String : Any]

        if let t = token {
            params = [
                "key" : AppDelegate.googlePlacesAPIKey,
                "pagetoken" : t,
            ]
        } else {
            params = [
                "key" : AppDelegate.googlePlacesAPIKey,
                "radius" : radius,
                "location" : "\(coordinates.latitude),\(coordinates.longitude)",
                "type" : category.lowercased()
            ]
        }

        Alamofire.request(searchApiHost, parameters: params, encoding: URLEncoding(destination: .queryString)).responseJSON { response in

            if let error = response.error {
                completion(nil, error)
            }
            if let response = QNearbyPlacesResponse(dic : response.result.value as? [String : Any]) {
                completion(response, nil)
            }
            else {
                completion(nil, QNearbyPlacesResponseError.noParsingDone)
            }
        }
    }

    static func googlePhotoURL(photoReference:String, maxWidth:Int) -> URL? {
        return URL.init(string: "\(googlePhotosHost)?maxwidth=\(maxWidth)&key=\(AppDelegate.googlePlacesAPIKey)&photoreference=\(photoReference)")
    }
}

enum QNearbyPlacesResponseError : Error {
    case noParsingDone
}


struct QNearbyPlacesResponse {
    var nextPageToken: String?
    var status: String  = "NOK"
    var places: [QPlace]?

    init?(dic:[String : Any]?) {
        nextPageToken = dic?["next_page_token"] as? String

        if let status = dic?["status"] as? String {
            self.status = status
        }

        if let results = dic?["results"] as? [[String : Any]]{
            var places = [QPlace]()
            for place in results {
                places.append(QPlace.init(placeInfo: place))
            }
            self.places = places
        }
    }

    func canLoadMore() -> Bool {
        if status == "OK" && nextPageToken != nil && nextPageToken?.characters.count ?? 0 > 0 {
            return true
        }
        return false
   }



}

and

struct QCategoryy {
    var name:String
    init(name:String) {
        self.name = name
    }
}

extension QCategoryy: ExpressibleByStringLiteral {
    init(stringLiteral value: String) {
        self.name = value
    }
    init(unicodeScalarLiteral value: String) {
        self.init(name: value)
    }
    init(extendedGraphemeClusterLiteral value: String) {
        self.init(name: value)
    }
}

it seems to me that it's all right, i can not understand why when i go into my uiviewcontroller the tableView has all the empty cells, here there is also a screen of what i see when running the project在我看来这没问题,我不明白为什么当我进入我的 uiviewcontroller 时,tableView 有所有空单元格,这里还有一个我在运行项目时看到的屏幕在此处输入图片说明

hope someone can find the issue希望有人能找到问题

尝试:

tableView.dataSource = self

Try adding:尝试添加:

tableView.dataSource = self
tableView.delegate = self`

to your viewDidLoad()到您的viewDidLoad()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM