English 中文(简体)
快速观察——表态发放单元
原标题:Swift iOS app - Issue formatting Cells for TableView

获取二次数据以显示我的可调意见。 我有3个数据来自APIC(姓名、地址、电话)。 没有出现“Name”的问题,但当我试图显示次要数据(地址、电话)时,我被困。 我在此有两份法典文件,一份是“Controller”文件,正在处理表文和一份处理数据分类的示范文件。 二级数据在我的代码中称为“尾矿”。 任何帮助都受到高度赞赏。

第一部《法典》档案是Gitroller,第二部是示范档案。

import UIKit

struct SportsItem {
    let facility: String
    let address: String
    let telephone: String
}

class SportsResultsViewController: UIViewController, UITableViewDataSource {
    var sportsData: [SportsItem] = []
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sportsData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
       
        cell.textLabel?.text = sportsData[indexPath.row].facility
        cell.detailTextLabel?.numberOfLines = 0
        cell.detailTextLabel?.text = "Address: (sportsData[indexPath.row].address)
Phone: (sportsData[indexPath.row].telephone)"
        
        cell.textLabel?.font = UIFont.systemFont(ofSize: 8)
        cell.detailTextLabel?.font = UIFont.systemFont(ofSize: 8)
        
        return cell
    }
    
    var jsonString: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        resultsTableView.dataSource = self
        resultsTableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellIdentifier")
        resultsTableView.cellLayoutMarginsFollowReadableWidth = true
        resultsTableView.rowHeight = UITableView.automaticDimension
        resultsTableView.estimatedRowHeight = 164.0
        
        
        
        if let jsonString = jsonString {
            if let data = jsonString.data(using: .utf8),
               let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
               let features = json["features"] as? [[String: Any]] {
                
                var sportsData: [SportsItem] = []
                
                for i in 0..<min(10, features.count) {
                    if let feature = features[i]["properties"] as? [String: Any]? ?? [:],
                       let address = feature["Address"] as? String,
                       let telephone = feature["Telephone"] as? String,
                       let facility = feature["Facility Name"] as? String {
                        
                        let sportsItem = SportsItem(facility: facility, address: address, telephone: telephone)
                        sportsData.append(sportsItem)
                    }
                }
                
                self.sportsData = sportsData
                
                if sportsData.isEmpty {
                    // Handle empty data case if needed
                }
                
                resultsTableView.reloadData()
            }
        }
    }
    


    @IBAction func findHotelsPressed(_ sender: UIButton) {
        UIApplication.shared.open(URL(string: "https://www.hotels.com/de606379/hotels-hong-kong-hong-kong-sar/")!)
    }
    
    @IBOutlet weak var resultsTableView: UITableView!
    
    
    
}

第二部法典文件。

import UIKit

struct SportsManager {
    let sportsURL = "https://geodata.gov.hk/gs/api/v1.0.0/geoDataQuery?q=%7Bv%3A%221%2E0%2E0%22%2Cid%3A%22d1a2e8e0-8636-41e9-b664-0e64b86860a2%22%2Clang%3A%22ALL%22%7D"
    
    func getSportsData(completion: @escaping (String?) -> Void) {
        guard let url = URL(string: sportsURL) else { return }
        
        URLSession.shared.dataTask(with: url) { (data, response, error) in
            if let error = error {
                print(error.localizedDescription)
                completion(nil)
                return
            }
            
            guard let data = data else {
                completion(nil)
                return
            }
            
            do {
                let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                let jsonString = try JSONSerialization.data(withJSONObject: jsonData ?? "", options: .prettyPrinted)
                let sportsData = String(data: jsonString, encoding: .utf8)
                completion(sportsData)
            } catch let error {
                print(error.localizedDescription)
                completion(nil)
            }
        }.resume()
    }
}

问题回答

暂无回答




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签