Pass data from a function to table view - ios

I have been trying to create an app that gets a website data through a function ( func obtainData) and display some of the data on a tableView.
I have figured out the part on how to get the data from the website then make it as an array so I can use indexpath.row but I have not able to find out the way to pass on the data I'm getting to display it on a tableView.
Any ideas!
Thanks
Below is the code I wrote.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var recommendation = ""
var recommendationArray = ""
var delExtraTextArray = [String]()
var urlRecommendationArrayStart = [String]()
var urlRecommendationArrayEnd = [String]()
var RecommendationStart = [String]()
var RecommendationEnd = [String]()
// need the var below to make the recommendations as an array to be used in a table view as indexpath.row
var cellNumber = [String]()
var cellTitle = [String]()
var cellDetails = [String]()
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
obtainData()
tableView.delegate = self
tableView.dataSource = self
}
func obtainData () {
var url = NSURL (string: "http://www.choosingwisely.org/societies/american-college-of-obstetricians-and-gynecologists")
if url != nil {
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error == nil {
// to get all of the url content and covert them into string
var urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding) as NSString!
// to get to a specific contect seperated by a string
self.urlRecommendationArrayStart = (urlContent?.componentsSeparatedByString("<ol class=\"society-ol\">"))!
if self.urlRecommendationArrayStart.count > 0 {
self.urlRecommendationArrayEnd = self.urlRecommendationArrayStart[1].componentsSeparatedByString("</ol>")
// print(self.urlRecommendationArrayEnd)
// to check if there is any extra not needed text at the end of the recommnedations in the source page
self.delExtraTextArray = self.urlRecommendationArrayEnd[0].componentsSeparatedByString("<p><a")
if self.delExtraTextArray.count > 0 {
self.recommendationArray self.delExtraTextArray[0] as! String
self.obtainRecommendationTitle()
} else {
self.recommendationArray = self.urlRecommendationArrayEnd[0] as! String
self.obtainRecommendationTitle()
// print("method 2 worked")
}
} else {
self.textView.text = "Sorry, couldn't get the recommendation at this point. Please make sure to download the updated version of the app"
}
} else {
self.textView.text = "Please check connection then try again"
}
})
task.resume()
} else {
self.textView.text = "Please check connection then try again"
}
}
// to get the title of each recommendation
func obtainRecommendationTitle() -> Array<String> {
for var i = 2; i < urlRecommendationArrayEnd[0].componentsSeparatedByString("<p>").count - delExtraTextArray.count ; i = i + 4 {
self.RecommendationStart = self.delExtraTextArray[0].componentsSeparatedByString("<p>")
self.RecommendationEnd = RecommendationStart[i].componentsSeparatedByString("</p>")
self.recommendationArray = self.RecommendationEnd[0] as! String
self.cellTitle.append(recommendationArray)
}
return cellTitle
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellTitle.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = cellTitle [indexPath.row]
return cell
}

You would pass it via the cellForRowAtIndexPath delegate method. This question is too open ended for a firm answer, but following along any half-decent online UITableView tutorial should do the trick.
A quick glance at this one appears to hit the basics: https://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/

I think what you need is to configure the contents of the table view cell with the data you want. Based on this assumption, you can use something like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("PUT_YOUR_CELL_IDENTIFIER_HERE") as? UITableViewCell {
let stuff = yourArray[indexPath.row]
stuff.some_property_you_want = the_value_you_want
return cell
} else {
return UITableViewCell()
}
}
If you show some code or explain your problem a little better, you will get a better support from the people here on Stack Overflow.
EDIT (based on your edit):
Are you using a normal cell?
The cell has the text field to put the string you want?
Did you define the cell's identifier, "cell", in the storyboard?
Did you connect the tableView outlet to the tableView itself?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell {
print("ENTERED HERE!")
let myCellText = cellTitle[indexPath.row]
//what is the result of this print?
print("TEXT TO ADD: \(myCellText)")
//(...)
cell.textLabel?.text = myCellText
return cell
} else {
return UITableViewCell()
}
}
What is the result of these prints?

Related

Delete cell from tableView and data from Firestore iOS

I'm making an restaurant app where I have restaurant which contains food. I have the following problem. From each restaurant, I can add food into my shopping cart, but when im trying to swipe left to delete a cell, close the cart and re-enter in the cart, is deleting all the cart, not the cell I have selected for swipping
This is my model :
struct Cart
{
var photoKeyCart: String
var foodCart: String
var priceCart: Int
}
This is the function im using for retrieving the data from Firestore and displaying it into the cart. This function is called in the viewdidload() .
func getCartProducts() {
let db = Firestore.firestore()
let userID = (Auth.auth().currentUser?.uid)!
db.collection("CartDatabase").document(userID).collection("CartItems").getDocuments { (document, error) in
if let error = error {
print(error)
return
} else {
for document in document!.documents {
let data = document.data()
let newEntry = Cart(photoKeyCart: data["photoKeyCart"] as! String, foodCart: data["foodCart"] as! String , priceCart: data["priceCart"] as! Int
)
self.cart.append(newEntry)
}
}
DispatchQueue.main.async {
// self.datas = self.filteredData
self.cartTableView.reloadData()
}
}
}
Viewdidload function :
override func viewDidLoad() {
super.viewDidLoad()
getCartProducts()
let nib = UINib(nibName: "CartTableViewCell", bundle: nil)
cartTableView.register(nib, forCellReuseIdentifier: "CartTableViewCell")
cartTableView.delegate = self
cartTableView.dataSource = self
}
Table view extension :
extension CartViewController: UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var sum = 0
for item in cart{
sum += item.priceCart
}
priceTotalLabel.text = "\(sum) lei"
return cart.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = cartTableView.dequeueReusableCell(withIdentifier: "CartTableViewCell", for: indexPath) as! CartTableViewCell
let carts = cart[indexPath.row]
let storageRef = Storage.storage().reference()
let photoRef = storageRef.child(carts.photoKeyCart)
cell.foodInCartPrice.text = " \(carts.priceCart) lei "
cell.foodInCartName.text = carts.foodCart
cell.foodInCartImage.sd_setImage(with: photoRef)
cell.foodInCartImage.layer.borderWidth = 1
cell.foodInCartImage.layer.masksToBounds = false
cell.foodInCartImage.layer.borderColor = UIColor.black.cgColor
cell.foodInCartImage.layer.cornerRadius = cell.foodInCartImage.frame.height/2
cell.foodInCartImage.clipsToBounds = true
return cell
}
This is the function im using for swiping left and trying to delete the cell, but it deletes all my cells. I think the problem is that is deleting all the data from the database, and when the function is called, it have nothing to retrieve.
Database used : Tap here

Detect Taps in Different areas of a tableViewCell

this one has me pretty well stumped so hoping someone can help. I have a tableview that has a custom cell with 3 labels and 2 buttons. If a user taps the label, i use didSelectRowAtIndexPath to set a variable from an array (i.e. variable = array[indexPath.row]) and then perform a segue, passing the info from the variable to a new VC with prepareForSegueWithIdentifier - this works fine and all is well in the world.
I'd like to implement the same functionality for the button within the same cell, however I'm having trouble detecting which row was tapped. I've tried an IBAction from the button where i code "func didSelectRowAtIndexPath" a second time, and it kind of works but the row is inaccurate (for example, it'll pass info for a cell a few below the one I actually tapped) -- I also tried a global variable that would get the row from the original didSelectRowAtIndexPath function but that had similar results.
What is the best practices for doing this? Is there another way to get the row i tapped from a second place within the tableView? Should i have used another label instead of a button?
Any help/ideas is greatly appreciated. Happy to post my code as well if need. Thank you!!
import UIKit
import Parse
var rowNumber:Int = 0
class FeedTableViewController: UITableViewController {
var postToWeb:String = ""
var usernames = [String]()
var linkArr = [String]()
var titleArray = [String]()
var users = [String: String]()
var createdAt = [String]()
var userList: [User] = []
var sortedArticles: [User] = []
var commentUrl:String = ""
var commentTitle: String = ""
var commentUser:String = ""
struct User {
var date:NSDate
var username:String
var url:String
var title:String
}
let cellSpacingHeight: CGFloat = 10
class commentButtonClass: UIButton {
var section:Int = 0
}
#IBAction func commentButtonPressed(sender: AnyObject) {
commentTitle = self.userList[rowNumber].title
commentUrl = userList[rowNumber].url
commentUser = (PFUser.currentUser()?.username)!
performSegueWithIdentifier("commentView", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
var query = PFUser.query()
query?.orderBySortDescriptor(NSSortDescriptor(key: "createdAt", ascending: false))
query?.findObjectsInBackgroundWithBlock({ (objects, error) in
if let users = objects {
self.usernames.removeAll(keepCapacity: true)
self.linkArr.removeAll(keepCapacity: true)
self.titleArray.removeAll(keepCapacity: true)
self.createdAt.removeAll(keepCapacity: true)
for object in users {
if let user = object as? PFUser {
self.users[user.objectId!] = user.username!
}
}
}
let getFollowedUsersQuery = PFQuery(className: "followers")
getFollowedUsersQuery.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
getFollowedUsersQuery.orderBySortDescriptor(NSSortDescriptor(key: "createdAt", ascending: false))
getFollowedUsersQuery.findObjectsInBackgroundWithBlock { (objects, error) in
if let objects = objects {
for object in objects {
let followedUser = object["following"] as! String
let query = PFQuery(className: "posts")
query.whereKey("userId", equalTo: followedUser)
query.orderBySortDescriptor(NSSortDescriptor(key: "createdAt", ascending: false))
query.findObjectsInBackgroundWithBlock({ (objects, error) in
//create a struct with createdAt as struct name since will be unique majority of time if include time and date.
if let objects = objects {
for object in objects {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
let dateString = dateFormatter.stringFromDate(object.createdAt!)
self.userList.append(User(date: object.createdAt!, username:self.users[object["userId"] as! String]!, url: object["linkURL"] as! String, title: object["title"] as! String))
self.userList.sortInPlace{ $0.date.compare($1.date) == NSComparisonResult.OrderedDescending }
self.tableView.reloadData()
}
}
})
}
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Set the spacing between sections
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return cellSpacingHeight
}
// Make the background color show through
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.clearColor()
return headerView
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return userList.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
let dateString = dateFormatter.stringFromDate(self.userList[indexPath.section].date)
myCell.titleLabel.text = self.userList[indexPath.section].title
myCell.userLabel.text = self.userList[indexPath.section].username
myCell.createdDateLabel.text = dateString
myCell.rowNumberLabel.tag = indexPath.section
// add border and color
myCell.backgroundColor = UIColor.whiteColor()
myCell.layer.borderColor = UIColor.clearColor().CGColor
myCell.layer.borderWidth = 2
myCell.layer.cornerRadius = 0
myCell.clipsToBounds = true
return myCell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!;
rowNumber = indexPath.section
postToWeb = userList[indexPath.section].url
print(postToWeb)
performSegueWithIdentifier("webView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "webView") {
// initialize new view controller and cast it as your view controller
let vc = segue.destinationViewController as! WebViewController
// your new view controller should have property that will store passed value
vc.passedValue = postToWeb
}
if (segue.identifier == "commentView") {
let commentVc = segue.destinationViewController as! commentsViewController
commentVc.passedCommentTitle = commentTitle
commentVc.passedCommentUrl = commentUrl
commentVc.passedCommentUsername = commentUser
}
}
in the above, the segue to "webView" works perfectly and the data is passed. In the commentView - data is passed, but it's incorrect.
To get the UITableViewCell that was tapped (as an Action of the Button), you have different options:
1) Use Target-Action method, something like:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell
//...More Code
myCell.myBtn.addTarget(self, action: "myActionMethod:", forControlEvents: .TouchUpInside)
//... More Code
return myCell
}
And then define your action method (note, yourTbl is an instance of UITableView):
func myActionMethod(sender:UIButton){
//base on the hierarchy of the UI
//Suppose in this case:
//(UITableView)->(UITableViewCell)->(ContentView)->UIButton then:
if let tableViewCell = sender.superview?.superview as? UITableViewCell, indexPath = yourTbl.indexPathForCell(tableViewCell){
//Here you have the IndexPath
}
}
2) The same Idea, but basically your going to set the Tag property of the UIButton (to your row cell), and using Target-Action on the sender (parameter) you get the tag of the button.
Update Sample:
https://github.com/Abreu0101/SampleTargetActionMethod
What is the best practices for doing this?
You need you delegate pattern. See below example.
The CustomTableViewCell class:
protocol CustomTableViewCellDelegate {
func customTableViewCell(cell: CustomTableViewCell, didTextInLabelChanged text: String)
}
class CustomTableViewCell: UITableViewCell {
var delegate: CustomTableViewCellDelegate?
func handeLabelTextChanged(text: String) {
delegate?.customTableViewCell!(self, didTextInLabelChanged: text)
}
// ... some other code
}
The CustomViewController class:
class CustomViewController {
// some code
}
class CustomViewController: UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = // dequeue the right cell
cell.delegate = self
return cell
}
}
class CustomViewController: CustomTableViewCellDelegate {
func customTableViewCell(cell: CustomTableViewCell, didTextInLabelChanged text: String) {
// do tableView update with changed text
}
}
As you see all "changed" (or "tapped", or whatever) logic is going on in you cell. To notify controller about this changes you simply create a protocol with all methods what you need (in this case i notify my controller about text in label changed), then you need to create a var delegate: YourDelegate? and call it where you need. Next step you need implement "handle" function in your controller. It's very simple. You just need implement the customTableViewCell() function from CustomTableViewCellDelegate and also set the delegate variable to self for CustomTableViewCell.
UPDATE
When a user taps a button within the cell, i need the specific info from that cell, based on the indexPath.row
Very easy. Remember that in your controller you implement the CustomTableViewCellDelegate? So, to get specific info from cell you can do this:
class CustomViewController: CustomTableViewCellDelegate {
func customTableViewCell(cell: CustomTableViewCell, didTextInLabelChanged text: String) {
// here you get `someVariable` from your cell.
// where `someVariable` you set in the `cellForRowAtIndexPath` controller's
// method
let info = cell.someVariable
// or, if for some reason you not save in your cell some data,
// but you need the `indexPath.row` to get this data you
// can find this `indexPath` like this:
if let indexPath = tableView.indexPathForCell(cell) {
let number = youArrayOfNumbers[indexPath.row]
}
}
}

iOS Swift: Getting repeated value while updating 2D Array in custom UITableView cell

I have a 2D Array which I want to populate in UITableView Custom Cell in a specific pattern.
//Retrieved from Parse backend
var myArray = [["Name1", "Age1"],["Name2", "Age2"],["Name3", "Age3"]]
//What I need is:
nameArray = ["Name1", "Name2", "Name3"]
ageArray = ["Age1", "Age2", "Age3]
So that I can use indexPath to fill the Name data in the custom UITableView cell For Ex: nameArray[indexPath.row]
I tried using the for in loop,
var nameArray = NSMutableArray()
var ageArray = NSMutableArray()
//Inside CellForRowAtIndexPath
for data in myArray {
self.nameArray.addObject(data[0])
self.ageArray.addObject(data[1])
}
cell.nameLabel.text = "\(nameArray[indexPath.row])"
cell.ageLabel.text = "\(ageArray[indexPath.row])"
But I am getting repetitive name and age label filled with Name1 and Age1 in both the cell. Does anyone know whats wrong in this?
Is there a better way to reload this data as needed?
// UPDATED FULL WORKING CODE Thanks to #l00phole who helped me solve the problem
class NewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tableView: UITableView!
var data = [[String]]()
var cost = Double()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
uploadData()
}
func uploadData() {
let query = PFQuery(className:"Booking")
query.getObjectInBackgroundWithId("X0aRnKMAM2") {
(orders: PFObject?, error: NSError?) -> Void in
if error == nil && orders != nil {
self.data = (orders?.objectForKey("orderDetails"))! as! [[String]]
//[["Vicky","21"],["Luke", "18"],["7253.58"]]
//*****Removing the last element as it is not needed in the tableView data
let count = self.data.count - 1
let c = self.data.removeAtIndex(count)
cost = Double(c[0])!
//******
} else {
print(error)
}
self.reloadTableData()
}
}
func reloadTableData()
{
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
return
})
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:NewTableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewTableViewCell
// Configure the cell...
cell.nameLabel.text = "\(data[indexPath.row][0])"
cell.ageLabel.text = "\(data[indexPath.row][1])"
return cell
}
You are adding to the nameArray and ageArray every time cellForRowAtIndexPath is called and you are not clearing them first. This seems inefficient and you should only populate those arrays when the input data changes, not when generating the cells.
I don't even think you need those arrays, as you could just do:
cell.nameLabel.text = "\(data[indexPath.row][0])"
cell.ageLabel.text = "\(data[indexPath.row][1])"
You don't have to create separate array for name and age, you can use the existing myArray as below
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:NewTableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewTableViewCell
// Configure the cell...
let dataArray = myArray[indexPath.row]
cell.nameLabel.text = "\(dataArray[0])"
cell.ageLabel.text = "\(dataArray[1])"
return cell
}
}

SWIFT: Difficultly displaying data in tableView

I am attempting to display data from Parse onto the following tableView controller. For some reason, the data is not displaying on the tableView (i.e. the rows are blank). I do not think that the data queried from Parse is being appended to the arrays. I am wondering what I'm doing wrong here.
Here's the current output:
I am using a custom prototype cell with identifier "CellTrack" class "TrackTableViewCell" and as shown below:
Here is my code in the TableViewController file:
import UIKit
import Parse
class MusicPlaylistTableViewController: UITableViewController {
var usernames = [String]()
var songs = [String]()
var dates = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
var query = PFQuery(className:"PlaylistData")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects! as? [PFObject] {
self.usernames.removeAll()
self.songs.removeAll()
self.dates.removeAll()
for object in objects {
let username = object["username"] as? String
self.usernames.append(username!)
print("added username")
let track = object["song"] as? String
self.songs.append(track!)
let date = object["createdAt"] as? String
self.dates.append(date!)
self.tableView.reloadData()
}
}
} else {
print(error)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return usernames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellTrack", forIndexPath: indexPath) as! TrackTableViewCell
cell.username.text = usernames[indexPath.row]
cell.songTitle.text = songs[indexPath.row]
cell.CreatedOn.text = dates[indexPath.row]
return cell
}
}
And here is my code in the "TrackTableViewCell.swift" class:
import UIKit
class TrackTableViewCell: UITableViewCell {
#IBOutlet weak var songTitle: UILabel!
#IBOutlet weak var username: UILabel!
#IBOutlet weak var CreatedOn: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Execute your tableView.reloadData() in main thread.
dispatch_async(dispatch_get_main_queue(), {
self.tableViewCell.reloadData()
})
Try doing a guard let to see if those values are actually coming back as string or not. My guess would be that the value for created at never came back. Try it out and let me know.
guard let username = object["username"] as? String else {
print ("could not get username")
}
self.usernames.append(username)
print("added username")
guard let track = object["song"] as? String else {
print ("could not get song")
return
}
self.songs.append(track)
guard let date = object["createdAt"] as? String else {
print ("could not get createdAt")
return}
self.dates.append(date!)
func dequeueReusableCellWithIdentifier(_ identifier: String) -> UITableViewCell?
Return Value
A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CellTrack", forIndexPath: indexPath) as! TrackTableViewCell
if cell == nil {
// create a new cell here
cell = TrackTableViewCell(...)
}
cell.username.text = usernames[indexPath.row]
cell.songTitle.text = songs[indexPath.row]
cell.CreatedOn.text = dates[indexPath.row]
return cell
}

Swift parse query results not appearing in tableview

I am running into difficulty displaying the data from the query I made in the individual cells of my tableview. I believe that my logic is correct, but I'm not seeing the console.log's that I am calling within my function that contains the Parse queried data. This might be a simple fix, but it isn't coming to me at the moment. The console log I should be seeing to validate that my query is coming through correctly is the println("\(objects.count) users are listed"), it should then be displayed within the usernameLabel.text property.
import UIKit
class SearchUsersRegistrationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var userArray : NSMutableArray = []
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
loadParseData()
}
func loadParseData() {
var query : PFQuery = PFUser.query()
query.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]!, error:NSError!) -> Void in
if error == nil {
if let objects = objects {
println("\(objects.count) users are listed")
for object in objects {
self.userArray.addObject(object)
}
self.tableView.reloadData()
}
} else {
println("There is an error")
}
}
}
let textCellIdentifier = "Cell"
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.userArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! SearchUsersRegistrationTableViewCell
let row = indexPath.row
let cellDataParse : PFObject = self.userArray.objectAtIndex(row) as! PFObject
//cell.userImage.image = UIImage(named: usersArr[row])
cell.usernameLabel.text = cellDataParse.objectForKey("_User") as! String
return cell
}
}
I fixed the issue. I needed to cast the index path row in the users array as a PFUser and then cast the user's username property as a String and then set that as the label text.
let row = indexPath.row
var user = userArray[row] as! PFUser
var username = user.username as String
cell.usernameLabel.text = username

Resources