Swift/PHP How to display mysql data (json) in UITableView using Alamofire

不问归期 提交于 2020-01-13 20:21:13

问题


I would like to create a TableView which return just one cell. Include in this cell three labels: id, username and category. This would result to let user to see his basic detail info.

When I Run the APP, my UITableView still not showing any result (blank).

Please anyone can review my code to make it works ? I can not see where I am wrong as I don't get any error in Xcode

TableView Controller

import UIKit
import Alamofire

class MyProfile: UITableViewController {

var users = [MyProfileBasicData]()

    override func viewDidLoad() {
    super.viewDidLoad()

    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        getData()
    }

    func getData() {
        let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
        var username = prefs.valueForKey("USERNAME") as NSString

        Alamofire.request(.GET, "http://mysite/app/data/jsonuser.php", parameters: ["username": username]).responseJSON() { (_, _, data, error) in if let jsonResult = data as? Array<Dictionary<String, String>> {
            var UserID = jsonResult [0]["Id"]
            var username1 = jsonResult [0]["username"]
            var category = jsonResult [0]["category"]

            for result in jsonResult {
                var userInfo = MyProfileBasicData()
                userInfo.Id = jsonResult [0]["Id"]
                userInfo.username = jsonResult [0]["username"]
                userInfo.category = jsonResult [0]["category"]
                self.users.append(userInfo)

            }

                dispatch_async(dispatch_get_main_queue(),

                    { self.tableView.reloadData()
                    })
                println(UserID)
                println(username1)
                println(category)
            }
            println(error)
        }
    func didReceiveMemoryWarning() {

    }

   func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return users.count
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:
        indexPath) as MyProfileCell

    var user = users[indexPath.row]
    cell.profileID?.text = user.Id
    cell.profileUsername?.text = user.username
    cell.profileCategory?.text = user.category

return cell
    }
  }

Table View Cell

import UIKit

class MyProfileCell: UITableViewCell {

    @IBOutlet weak var profileCategory: UILabel!
    @IBOutlet weak var profileID: UILabel!
    @IBOutlet weak var profileUsername: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

    }
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

}

Data

import Foundation

class MyProfileBasicData: NSObject {

    var Id:String?
        var username:String?
        var category:String?


    }

回答1:


The problem is that numberOfSectionsInTableView and numberOfRowsInSection are defined as methods inside getData. If you pull them out of the getData method, they should be called successfully. Note, when you pull them out, the compiler should now complain that you need to add override (like you have for cellForRowAtIndexPath).


My original response based upon standard table view issues is below.


Two possible issues are:

  1. Failure to specify the table view's delegate:

    If this was the case, your UITableViewDataDelegate methods will not get called at all. Put breakpoints/log statements in those routines and confirm they're getting called. If not, confirm that the table view's delegate and data source properties have been set properly.

  2. Failure to hook up the @IBOutlet references in the UITableViewCell subclass to the prototype cell.

    If this was the case, the custom cell properties, e.g. cell.profileID, etc., will be nil in cellForRowAtIndexPath. If they are nil, then go back to Interface Builder and hook them up.



来源:https://stackoverflow.com/questions/28646290/swift-php-how-to-display-mysql-data-json-in-uitableview-using-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!