Reputation: 887
I am trying to retrive each row from database and keep value in dictionary. After that I have created a array of dictionary. But when i am trying to show each value of dictionary in table cell, i can't . Here is my code
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var borrowerInformation:NSMutableDictionary? = NSMutableDictionary();
var borrowerName:String = ""
var givenDate:String = ""
var borrower:NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
dataQuery()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.borrower.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BorrowerLanderTableViewCell
cell.borrowerName.text = self.borrower[indexPath.row] as? String
return cell
}
func dataQuery(){
let queryBorrower = PFQuery(className:"BorrowerLander")
queryBorrower.whereKey("borrowerLander", equalTo: 0)
queryBorrower.whereKey("userId", equalTo: (PFUser.currentUser()?.objectId)!)
queryBorrower.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
self.borrower = NSMutableArray()
if error == nil {
if let borrowerData = objects {
for object in borrowerData {
self.borrowerName = object["name"] as! String
self.givenDate = object["givenDate"] as! String
self.borrowerInformation = [ "borrowerName": self.borrowerName,"givenDate":"self.givenDate"]
self.borrower.addObject(self.borrowerInformation!)
}
}
}
self.tableview.reloadData()
}
}
}
Here, i have just added main part of the code. Please tell me how can i show value of array of the dictionary in table view cell?
Upvotes: 1
Views: 95
Reputation: 7906
I don't know how you did the setup but an issue could be that you have not set your tableviews delegate and datasource to the controller you are working with.
self.tableView.dataSource = self
self.tableView.delegate = self
I'm also wondering if you did implement the numberOfRows and numberOfSections delegate methods.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return borrower.count
}
and
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
Also when setting the text you need to actually use the name string you added to the dictionary
let borrower = self.borrower[indexPath.row]
if let name = borrower["borrowerName"] as? String{
cell.borrowerName.text = name
} esle {
cell.borrowerName.text = "error"
}
Also declare your data array as
var borrower:[[String: AnyObject]] = []
Extra edit:
I do want to say that I really think you should use a custom object here instead of an dictionary
class Borrower {
var name: String
var givenDate: String
//etc.
init(name: String, givenDate: String) {
self.name = name
self.givenDate = givenDate
}
}
enabling the following
var borrower:[Borrower] = []
and in the query:
for object in borrowerData {
if let name = object["name"] as? String, givenDate = object["givenDate"] as? String {
self.borrower.append(Borrower(name: name, givenDate: giverDate))
}
}
Upvotes: 2
Reputation: 24466
First, make sure you've set the dataSource
and delegate
for your table view to your view controller. Then, the function tableView:cellForRowAtIndexPath:
will never get called unless you implement tableView:numberOfRowsInSection
. The default implementation of that function returns 0. Try implementing this:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return borrower.count
}
Also, if the code inside of queryBorrower.findObjectsInBackgroundWithBlock {
is actually in the background, you need to dispatch your table view reload to the main thread. Like this:
dispatch_async(dispatch_get_main_queue(), { [weak self] () -> Void in
self?.tableView.reloadData()
})
Upvotes: 1
Reputation: 1633
You need to implement numberOf...
dataSource delegate first:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
Upvotes: 0