【问题标题】:Cell data not showing up in UITableView?单元格数据未显示在 UITableView 中?
【发布时间】:2016-06-30 01:20:58
【问题描述】:

知道如何使用 Alamofire 发出这个 GET 请求 - 得到我想要的东西 - 将信息传递给 func tableView(tableView: UITableView - 填充并返回每个单元格,但我的表格不会显示加载的数据?

从 AlamoFire 承诺的回报中,我调用 self.refresh(),它在主线程中调用它:

func refresh() {

        dispatch_async(dispatch_get_main_queue(),{
            self.tableView.reloadData()
        });

-- 有任何想法吗?这简直让我发疯。提前感谢您的任何想法或解决方案!

import UIKit
import Alamofire
import MapKit

class ListViewController: UIViewController, UISearchBarDelegate, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource {

    var tools = [Tool]()


    @IBOutlet weak var searchBar: UISearchBar!


    @IBOutlet weak var tableView: UITableView!

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


    override func viewDidLoad() {
        super.viewDidLoad()

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

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 116
        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()

    }

    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

    }

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

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

        let searchTerm = String(searchBar.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.refresh()

//                    })

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

    }

    func refresh() {

        dispatch_async(dispatch_get_main_queue(),{
            self.tableView.reloadData()
        });

    }

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

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

     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]

        if(indexPath.row==0){
        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
    }
}

【问题讨论】:

  • 你得到什么输出? numberOfRowsInSection 被调用了吗? cellForRowAtIndexPath 被调用了吗?您的表格视图是否在屏幕上(例如,约束设置正确)?
  • 是的,是的,这就是令人困惑的地方。关于适当的约束 - 我如何在情节提要上确保这一点?
  • 我通常做的第一件事是将背景颜色设置为亮绿色或其他东西,以便您可以查看 tableview 是否可见。检查情节提要中的橙色或红色约束警告。还要检查您的 cell.title 属性是否为 nil - 在 cellForRowAtIndexPath 中设置断点并检查
  • (lldb) po cell.title.text 致命错误:在展开可选值错误时意外发现 nil:执行被中断,原因:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)。
  • 这是否表明标签未正确分配?

标签: ios xcode swift uitableview alamofire


【解决方案1】:

确保在attribute inspector! 中设置Reuse Identifier,在我的例子中是'ToolTableViewCell'

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多