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?
}
cellForRowAtIndexPathis not defined inside the class. I'm sure this is a cut and paste error (because otherwise, how could you referenceusers?), but still, you should confirm.responseJSONblock should make sure to empty/resetusersbefore adding anything to it. TheviewDidAppearcan be called multiple times (e.g. if you push to another view controller and then pop back to this one,viewDidAppearis called again).cellForRowAtIndexPathis fine (it's in the class), but you've implementednumberOfSectionsInTableViewandnumberOfRowsInSectionas private methods insidegetData. The closing brace forgetDataappears to have been accidentally been placed after these other three functions. Pull these functions out (or move the closing brace for thegetDatafunction). By the way, if you select all of the text (press command+a) and then reformat (press control-i), it will adjust the indentation and the problem should be self-evident.users, before theresponseJSONblock adds any new data to the array, it would reset it withself.users.removeAll(keepCapacity: true)orself.users = [MyProfileBasicData]().