ios - 为什么我的单元格数据没有显示在 UITableView 中?

标签 ios xcode swift

我的单元格已准确填充!这怎么没有加载到 tableview 中??

我的数据为什么不显示?这怎么可能?

我的返回单元格按需要打印出来 - 是什么将它们推到桌面上?

import UIKit
import Alamofire
import MapKit
//import CoreLocation

class ToolTableViewController: UITableViewController, CLLocationManagerDelegate {

//    @IBOutlet weak var tableview: UITableView!
    var jsonArray:NSMutableArray?
    var tools = [Tool]()

    @IBOutlet weak var toolListSearchBar: UISearchBar!

    let locationManager = CLLocationManager()
    var currentLat: CLLocationDegrees = 0.0
    var currentLong: CLLocationDegrees = 0.0



    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
//        print("locations = \(locValue.latitude) \(locValue.longitude)")
        let location = locations.last! as CLLocation
        currentLat = location.coordinate.latitude
        currentLong = location.coordinate.longitude
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.dataSource = self
        self.tableView.delegate = self

        self.tableView.registerClass(ToolTableViewCell.self, forCellReuseIdentifier: "ToolTableViewCell")
        self.locationManager.delegate = self
        self.locationManager.requestAlwaysAuthorization()

        self.locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()

        }
        else{
            print("Location service disabled");
        }

    }


    func searchBarSearchButtonClicked(searchbar: UISearchBar)
    {
        searchbar.resignFirstResponder()
        tools = []

        let defaults = NSUserDefaults.standardUserDefaults()
        let userid: Int = defaults.objectForKey("toolBeltUserID") as! Int

        let searchTerm = String(toolListSearchBar.text!)
        print(searchTerm)


        Alamofire.request(.GET, "http://localhost:3000/tools/search", parameters: ["keyword": searchTerm, "latitude": currentLat, "longitude": currentLong,
            "user": userid]) .responseJSON {response in
            if let JSON = response.result.value {
                print("\(JSON)")

               for i in 0 ..< JSON.count {

                let owner = JSON[i].objectForKey("owner")
                let tool = JSON[i].objectForKey("tool")
                let title = tool!["title"] as! String!
                let ownerId = owner!["id"] as! Int!
                let distanceToTool = JSON[i].objectForKey("distance") as! Double
                var description: String

                    if let des = tool!["description"] as?  NSNull {
                        description = ""
                    } else {
                        description = (tool!["description"] as? String!)!
                    }

                    let myTool = Tool(title: title!, description: description, ownerId: ownerId!, distance: distanceToTool)


                    self.tools.append(myTool)

                }

                dispatch_async(dispatch_get_main_queue(), {

                    self.tableView.reloadData()

                })

            } else {
                print("Sent search term, but no response")
            }
        }

    }

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

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "ToolTableViewCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ToolTableViewCell
        let tool = tools[indexPath.row]

        cell.title?.text = tool.title
//       print(tool.title)
//        cell.toolListDescription?.text = tool.description
        cell.ownerId = tool.ownerId
//        print(tool.ownerId)
//       print(tool.distance)
//        cell.distance?.text = "\(tool.distance)mi"

        print(cell)
        return cell
    }

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

}

最佳答案

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    }

您可以添加此代码吗?

if let cell = nil {
    cell = you cell init?
}

关于ios - 为什么我的单元格数据没有显示在 UITableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38088454/

相关文章:

ios - UIBezierPath 缩放所有坐标

ios - xcode 6 [swift] 中带有分段控件的多个用户界面

ios - swift promise 套件 : Equivalent to when() which executes sequentially?

xcode - 在 Xcode 7 中的项目之间导航

ios - CABasicAnimation 在动画开始前快速跳转

ios - 无法将符合协议(protocol)的值设置为具有协议(protocol)类型的属性

ios - 在 Xcode 中,为什么特定的部署目标会破坏我的代码,我应该如何使用它?

ios - 在状态之间向 UIButton 添加动画

ios - 完成转换时的 io7 导航检查

xcode - 从 Xcode 断点运行 shell 命令