Swift/PHP如何使用Alamofire在UITableView中显示mysql数据(json) [英] Swift/PHP How to display mysql data (json) in UITableView using Alamofire
问题描述
我想创建一个只返回一个单元格的TableView.在此单元格中包含三个标签:id,用户名和类别.这将导致用户查看其基本详细信息.
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.
当我运行APP时,我的UITableView仍然没有显示任何结果(空白).
When I Run the APP, my UITableView still not showing any result (blank).
请任何人都可以查看我的代码以使其起作用?我看不到哪里错了,因为我没有在Xcode中遇到任何错误
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控制器
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
}
}
表格视图单元格
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)
}
}
数据
import Foundation
class MyProfileBasicData: NSObject {
var Id:String?
var username:String?
var category:String?
}
推荐答案
问题是numberOfSectionsInTableView
和numberOfRowsInSection
被定义为方法 inside getData
.如果将它们从getData
方法中拉出,则应成功调用它们.请注意,将它们拉出时,编译器现在应该抱怨您需要添加override
(就像您对cellForRowAtIndexPath
一样).
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.
两个可能的问题是:
-
无法指定表视图的委托:
Failure to specify the table view's delegate:
如果是这种情况,您的UITableViewDataDelegate
方法将根本不会被调用.将断点/日志语句放在这些例程中,并确认它们已被调用.如果没有,请确认表视图的委托和数据源属性已正确设置.
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.
无法将UITableViewCell
子类中的@IBOutlet
引用连接到原型单元.
Failure to hook up the @IBOutlet
references in the UITableViewCell
subclass to the prototype cell.
如果是这种情况,则自定义单元格属性(例如cell.profileID
等在cellForRowAtIndexPath
中将是nil
.如果它们是nil
,则返回Interface Builder并将其连接起来.
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.
这篇关于Swift/PHP如何使用Alamofire在UITableView中显示mysql数据(json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!