gpt4 book ai didi

ios - TableView 不显示单元格中的内容 (Swift)

转载 作者:行者123 更新时间:2023-11-28 06:11:00 25 4
gpt4 key购买 nike

我创建了一个带有 tableView 的 UIViewController 类来显示单元格中的不同位置,我正在使用 Alamofire 和 Google API( map 、地点),唯一的问题是当我运行项目时单元格是空的,这是我的 Controller 类:

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)
}
}

这些是使用它的两个类:

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
}



}

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)
}
}

在我看来没关系,我不明白为什么当我进入我的 uiviewcontroller 时 tableView 有所有空单元格,这里还有一个屏幕显示我在运行项目时看到的内容 enter image description here

希望有人能找到问题

最佳答案

尝试:

tableView.dataSource = self

关于ios - TableView 不显示单元格中的内容 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46603409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com