I have three different types of cells in a tableViewController. I get which type of cell to have and an objectId of an item from a different class. I then go to each cell in the cellForRowAt method and load whatever the data is. This method has led me to 2 problems: 1) the dynamic height of one of the cell does not work because it's label's text is not found until after the cell is made. 2) All the cells have a "jumpy" (I can see the rows being populated as I scroll down, I guess because its loading the content every scroll) look as I scroll down the tableview.
So I want to preload all of the data before and put it in the cellForRowAt instead of searching for the data in cellForRowAt. This will fix both problems, but I have no idea how to do this. Based on my coding knowledge I would place the information that would go in each cell in arrays then populate the cells accordingly, but I do not know how to do this when using 3 different cells because to put the information in the cells from the array I would need to use indexPath.row; which I can not do this because I am loading 3 different types of data and adding them to different arrays so the indexPaths will not be aligned properly. This is the only way I can think of doing this and it's wrong. How can I fix this problem?
I have copied my code at the bottom so you can see what how I am loading the cells now and maybe you can get an understanding of how to fix my issue:
func loadNews() {
//start finding followers
let followQuery = PFQuery(className: "Follow")
followQuery.whereKey("follower", equalTo: PFUser.current()?.objectId! ?? String())
followQuery.findObjectsInBackground { (objects, error) in
if error == nil {
//clean followArray
self.followArray.removeAll(keepingCapacity: false)
//find users we are following
for object in objects!{
self.followArray.append(object.object(forKey: "following") as! String)
}
self.followArray.append(PFUser.current()?.objectId! ?? String()) //so we can see our own post
//getting related news post
let newsQuery = PFQuery(className: "News")
newsQuery.whereKey("user", containedIn: self.followArray) //find this info from who we're following
newsQuery.limit = 30
newsQuery.addDescendingOrder("createdAt") //get most recent
newsQuery.findObjectsInBackground(block: { (objects, error) in
if error == nil {
//clean up
self.newsTypeArray.removeAll(keepingCapacity: false)
self.objectIdArray.removeAll(keepingCapacity: false)
self.newsDateArray.removeAll(keepingCapacity: false)
for object in objects! {
self.newsTypeArray.append(object.value(forKey: "type") as! String) //get what type (animal / human / elements)
self.objectIdArray.append(object.value(forKey: "id") as! String) //get the object ID that corresponds to different class with its info
self.newsDateArray.append(object.createdAt) //get when posted
}
self.tableView.reloadData()
} else {
print(error?.localizedDescription ?? String())
}
})
} else {
print(error?.localizedDescription ?? String())
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let type = newsTypeArray[indexPath.row]
if type == "element" {
let cell = tableView.dequeueReusableCell(withIdentifier: "ElementCell") as! ElementCell
let query = query(className: "Element")
query.whereKey("objectId", equalTo: self.objectIdArray[indexPath.row])
query.limit = 1
query.findObjectsInBackground(block: { (objects, error) in
if error == nil {
for object in objects! {
let name = (object.object(forKey: "type") as! String)
let caption = (object.object(forKey: "caption") as! String) //small description (usually 2 lines)
cell.captionLabel.text = caption
}
} else {
print(error?.localizedDescription ?? String())
}
})
return cell
} else if type == "human" {
let cell = tableView.dequeueReusableCell(withIdentifier: "HumanCell") as! HumanCell
let query = query(className: "Human")
query.whereKey("objectId", equalTo: self.objectIdArray[indexPath.row])
query.limit = 1
query.findObjectsInBackground(block: { (objects, error) in
if error == nil {
for object in objects! {
let name = (object.object(forKey: "name") as! String)
let caption = (object.object(forKey: "caption") as! String) //small description (1 line)
cell.captionLabel.text = caption
}
} else {
print(error?.localizedDescription ?? String())
}
})
return cell
} else { //its an animal cell
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! AnimalCell
let query = query(className: "Animals")
query.whereKey("objectId", equalTo: self.objectIdArray[indexPath.row])
query.limit = 1
query.findObjectsInBackground(block: { (objects, error) in
if error == nil {
for object in objects! {
let caption = (object.object(forKey: "caption") as! String) //large description of animal (can be 2 - 8 lines)
cell.captionLabel.text = caption
}
} else {
print(error?.localizedDescription ?? String())
}
})
return cell
}
}
----- Edit ------
Implementation of #Woof's Logic:
In separate swift file:
class QueryObject {
var id: String?
var date: Date?
var userID : String?
var name: String?
}
class Element: QueryObject {
var objectID : String?
var owner : String?
var type : String?
var ability : String?
var strength : String?
}
class Human: QueryObject {
var objectID : String?
var follower : String?
var leader : String?
}
class Animal: QueryObject {
var objectID : String?
var type: String?
var owner : String?
var strength : String?
var speed : String?
var durability : String?
}
In TableviewController:
var tableObjects: [QueryObject] = []
func loadNews() {
//start finding followers
let followQuery = PFQuery(className: "Follow")
followQuery.whereKey("follower", equalTo: PFUser.current()?.objectId! ?? String())
followQuery.findObjectsInBackground { [weak self](objects, error) in
if error == nil {
//clean followArray
self?.followArray.removeAll(keepingCapacity: false)
//find users we are following
for object in objects!{
self?.followArray.append(object.object(forKey: "following") as! String)
}
self?.followArray.append(PFUser.current()?.objectId! ?? String()) //so we can see our own post
//this is a custom additional method to make a query
self?.queryNews(name: "News", followArray: self?.followArray ?? [], completionHandler: { (results) in
//if this block is called in a background queue, then we need to return to the main one before making an update
DispatchQueue.main.async {
//check that array is not nil
if let objects = results {
self?.tableObjects = objects
self?.tableView.reloadData()
}else{
//objects are nil
//do nothing or any additional stuff
}
}
})
} else {
print(error?.localizedDescription ?? String())
}
}
}
//I've made the code separated, to make it easy to read
private func queryNews(name: String, followArray: [String], completionHandler: #escaping (_ results: [QueryObject]?) -> Void) {
//making temp array
var temporaryArray: [QueryObject] = []
//getting related news post
let newsQuery = PFQuery(className: "News")
newsQuery.whereKey("user", containedIn: followArray) //find this info from who we're following
newsQuery.limit = 30
newsQuery.addDescendingOrder("createdAt") //get most recent
newsQuery.findObjectsInBackground(block: { [weak self] (objects, error) in
if error == nil {
//now the important thing
//we need to create a dispatch group to make it possible to load all additional data before updating the table
//NOTE! if your data are large, maybe you need to show some kind of activity indicator, otherwise user won't understand what is going on with the table
let dispathGroup = DispatchGroup()
for object in objects! {
//detecting the type of the object
guard let type = object.value(forKey: "type") as? String else{
//wrong value or type, so don't check other fields of that object and start to check the next one
continue
}
let userID = object.value(forKey: "user") as? String
let id = object.value(forKey: "id") as? String
let date = object.createdAt
//so now we can check the type and create objects
//and we are entering to our group now
dispathGroup.enter()
switch type {
case "element":
//now we will make a query for that type
self?.queryElementClass(name: "element", id: id!, completionHandler: { (name, objectID, owner, type, ability, strength) in
//I've added a check for those parameters, and if they are nil, I won't add that objects to the table
//but you can change it as you wish
if let objectName = name, let objectsID = objectID {
//now we can create an object
let newElement = Element()
newElement.userID = userID
newElement.id = id
newElement.date = date
newElement.objectID = objectID
newElement.owner = owner
newElement.type = type
newElement.ability = ability
newElement.strength = strength
temporaryArray.append(newElement)
}
//don't forget to leave the dispatchgroup
dispathGroup.leave()
})
case "human":
//same for Human
self?.queryHumanClass(name: "human", id: id!, completionHandler: { (name, objectID, follower, leader) in
if let objectName = name, let objectsID = objectID {
let newHuman = Human()
newHuman.userID = userID
newHuman.id = id
newHuman.date = date
temporaryArray.append(newHuman)
}
//don't forget to leave the dispatchgroup
dispathGroup.leave()
})
case "animal":
//same for animal
self?.queryAnimalClass(name: "animal", id: id!, completionHandler: { (name, objectID, type, owner, strength, speed, durability) in
if let objectName = name, let objectCaption = caption {
let newAnimal = Animal()
newAnimal.userID = userID
newAnimal.id = id
newAnimal.date = date
temporaryArray.append(newAnimal)
}
//don't forget to leave the dispatchgroup
dispathGroup.leave()
})
default:
//unrecognized type
//don't forget to leave the dispatchgroup
dispathGroup.leave()
}
}
//we need to wait for all tasks entered the group
//you can also add a timeout here, like: user should wait for 5 seconds maximum, if all queries in group will not finished somehow
dispathGroup.wait()
//so we finished all queries, and we can return finished array
completionHandler(temporaryArray)
} else {
print(error?.localizedDescription ?? String())
//we got an error, so we will return nil
completionHandler(nil)
}
})
}
//the method for making query of an additional class
private func queryElementClass(name: String, id: String, completionHandler: #escaping (_ name: String?, _ objectID: String?, _ owner: String?, _ type: String?, _ ability: String?, _ strength: String?) -> Void) {
let query = PFQuery(className: "Elements")
query.whereKey("objectId", equalTo: id)
query.limit = 1
query.findObjectsInBackground { (objects, error) in
if error == nil {
if let object = objects?.first {
let name = object.object(forKey: "type") as? String
let objectID = object.object(forKey: "objectID") as? String
let owner = object.object(forKey: "owner") as? String
let type = object.object(forKey: "type") as? String
let ability = object.object(forKey: "ability") as? String
let strength = object.object(forKey: "strength") as? String
completionHandler(name, objectID, owner, type, ability, strength)
} else {
print(error?.localizedDescription ?? String())
completionHandler(nil, nil, nil, nil, nil, nil)
}
} else {
print(error?.localizedDescription ?? String())
}
}
}
//the method for making query of an additional class
private func queryHumanClass(name: String, id: String, completionHandler: #escaping (_ name: String?, _ objectID: String?, _ follower: String?, _ leader: String?) -> Void) {
let query = PFQuery(className: "Human")
query.whereKey("objectId", equalTo: id)
query.limit = 1
query.findObjectsInBackground(block: { (objects, error) in
if let object = objects?.first {
let name = object.object(forKey: "type") as? String
let objectID = object.object(forKey: "objectID") as? String
let follower = object.object(forKey: "follower") as? String
let leader = object.object(forKey: "leader") as? String
completionHandler(name, objectID, follower, leader)
} else {
print(error?.localizedDescription ?? String())
completionHandler(nil, nil, nil, nil)
}
})
}
//the method for making query of an additional class
private func queryAnimalClass(name: String, id: String, completionHandler: #escaping (_ name: String?, _ objectID: String?, _ owner: String?, _ type: String?, _ strength: String?, _ speed: String?, _ durability: String?) -> Void) {
let query = PFQuery(className: "Animals")
query.whereKey("objectId", equalTo: id)
query.limit = 1
query.findObjectsInBackground(block: { (objects, error) in
if let object = objects?.first {
let name = object.object(forKey: "type") as? String
let objectID = object.object(forKey: "objectID") as? String
let owner = object.object(forKey: "owner") as? String
let strength = object.object(forKey: "strength") as? String
let type = object.object(forKey: "type") as? String
let speed = object.object(forKey: "speed") as? String
let durability = object.object(forKey: "durability") as? String
completionHandler(name, objectID, owner, type, strength, speed, durability)
} else {
print(error?.localizedDescription ?? String())
completionHandler(nil, nil, nil, nil, nil, nil, nil)
}
})
}
Looking at your projects I see multiple arrays with different data. It is very hard to edit your code with this kind of structure.
I would make it in this way:
1) create objects to store values, like structs/classes Animal, Human, Element. If they have same values like ids or whatever, you can create a super class Object and make other objects as subclasses
2) create one array as a data source for your table with objects not values
//if there is no super class
var objects:[AnyObject] = []
Or
//for the superclass
var objects:[YourSuperClass] = []
In the code below I will use Superclass, but you can change it to AnyObject
3) make a method to fill this array of objects before updating the table:
//I think it is better to use clousures and make data fetching in different queue
func loadNews(completionHandler: #escaping (_ objects: [YourSuperClass]) -> Void){
yourBackgroundQueue.async{
var objects = // fill here the array with objects
// it is important to return data in the main thread to make an update
DispatchQueue.main.async{
completion(objects)
}
}
}
And to fill our datasourse array, call this method when you need:
func updateTable(){
loadNews(){ [weak self] objects in
self?.objects = objects
self?.tablewView.reloadData()
}
So now you have an array of objects
4)We can use downcast to the specific class to set cells:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let object = objects[indexPath.row]
//making downcast
if let animal = object as? Animal,
let cell = tableView.dequeueReusableCell(withIdentifier: "AnimalCell") as? AnimalCell
//now you can fill the cell by properties than Animal object has
//return cell
return cell
}
if let human = object as? Human,
let cell = tableView.dequeueReusableCell(withIdentifier: "HumanCell") as? HumanCell
//do stuff with HumanCell
//return cell
return cell
}
//same way you can detect and fill any other cells
//this will be return an empty cell if there will be an object in the array that wasn't recognized. In this case the app won't crash, but you will see that something is wrong
return UITableViewCell()
}
So main thoughts:
make full loading before updating in separated queue (there may be exceptions, like if you would have to load images and don't what to wait all images to be downloaded before showing the table, it is better to fill the cells using simple values and then make an image loading inside each cell and show some activity indicator for each one)
create an array of objects with parameters, instead of making several arrays with simple values
use an array of objects to determine a cell type in the table.
============EDIT================
NOTE! I've made that code in the playground without importing PFQuery
If there will be errors, let me know. If you will stuck, let me know, maybe I will check your project directly
So, new code
//declaring Objects in separated file
class QueryObject {
var id: String?
var date: Date? //change of your date for object.createdAt has different type
var caption: String?
var name: String?
// var type: String? //use this var you don't need to have subclasses
}
//If your subclasses will not have unique parameters, you can left only one class QueryObject, without subclasses
//In this case just uncomment the "type" variable in the QueryObject, then you can check that var in cellForRowAt
class Animal: QueryObject {
//add any additional properties
}
class Human: QueryObject {
//add any additional properties
}
class Element: QueryObject {
//add any additional properties
}
class YourController: UITableViewController {
//allocate var inside ViewController
var tableObjects: [QueryObject] = []
func loadNews() {
//start finding followers
let followQuery = PFQuery(className: "Follow")
followQuery.whereKey("follower", equalTo: PFUser.current()?.objectId! ?? String())
followQuery.findObjectsInBackground { [weak self](objects, error) in
if error == nil {
//clean followArray
self?.followArray.removeAll(keepingCapacity: false)
//find users we are following
for object in objects!{
self?.followArray.append(object.object(forKey: "following") as! String)
}
self?.followArray.append(PFUser.current()?.objectId! ?? String()) //so we can see our own post
//this is a custom additional method to make a query
self?.queryNews(name: "News", followArray: self?.followArray ?? [], completionHandler: { (results) in
//if this block is called in a background queue, then we need to return to the main one before making an update
DispatchQueue.main.async {
//check that array is not nil
if let objects = results {
self?.tableObjects = objects
self?.tableView.reloadData()
}else{
//objects are nil
//do nothing or any additional stuff
}
}
})
} else {
print(error?.localizedDescription ?? String())
}
}
}
//I've made the code separated, to make it easy to read
private func queryNews(name: String, followArray: [String], completionHandler: #escaping (_ results: [QueryObject]?) -> Void) {
//making temp array
var temporaryArray: [QueryObject] = []
//getting related news post
let newsQuery = PFQuery(className: "News")
newsQuery.whereKey("user", containedIn: followArray) //find this info from who we're following
newsQuery.limit = 30
newsQuery.addDescendingOrder("createdAt") //get most recent
newsQuery.findObjectsInBackground(block: { [weak self] (objects, error) in
if error == nil {
//now the important thing
//we need to create a dispatch group to make it possible to load all additional data before updating the table
//NOTE! if your data are large, maybe you need to show some kind of activity indicator, otherwise user won't understand what is going on with the table
let dispathGroup = DispatchGroup()
for object in objects! {
//detecting the type of the object
guard let type = object.value(forKey: "type") as? String else{
//wrong value or type, so don't check other fields of that object and start to check the next one
continue
}
let id = object.value(forKey: "id") as? String
let date = object.createdAt
//so now we can check the type and create objects
//and we are entering to our group now
dispathGroup.enter()
switch type {
case "animal":
//now we will make a query for that type
self?.queryAdditionalClass(name: "Animals", id: id, completionHandler: { (name, caption) in
//I've added a check for those parameters, and if they are nil, I won't add that objects to the table
//but you can change it as you wish
if let objectName = name, let objectCaption = caption {
//now we can create an object
let newAnimal = Animal()
newAnimal.id = id
newAnimal.date = date
temporaryArray.append(newAnimal)
}
//don't forget to leave the dispatchgroup
dispathGroup.leave()
})
case "human":
//same for Human
self?.queryAdditionalClass(name: "Human", id: id, completionHandler: { (name, caption) in
if let objectName = name, let objectCaption = caption {
let newHuman = Human()
newHuman.id = id
newHuman.date = date
temporaryArray.append(newHuman)
}
//don't forget to leave the dispatchgroup
dispathGroup.leave()
})
case "elements":
//same for Element
self?.queryAdditionalClass(name: "Element", id: id, completionHandler: { (name, caption) in
if let objectName = name, let objectCaption = caption {
let newElement = Element()
newElement.id = id
newElement.date = date
temporaryArray.append(newElement)
}
//don't forget to leave the dispatchgroup
dispathGroup.leave()
})
default:
//unrecognized type
//don't forget to leave the dispatchgroup
dispathGroup.leave()
}
}
//we need to wait for all tasks entered the group
//you can also add a timeout here, like: user should wait for 5 seconds maximum, if all queries in group will not finished somehow
dispathGroup.wait()
//so we finished all queries, and we can return finished array
completionHandler(temporaryArray)
} else {
print(error?.localizedDescription ?? String())
//we got an error, so we will return nil
completionHandler(nil)
}
})
}
//the method for making query of an additional class
private func queryAdditionalClass(name: String, id: String, completionHandler: #escaping (_ name: String?, _ caption: String?) -> Void) {
let query = PFQuery(className: name)
query.whereKey("objectId", equalTo: id)
query.limit = 1
query.findObjectsInBackground(block: { (objects, error) in
if let object = objects?.first {
let name = object.object(forKey: "type") as? String
let caption = object.object(forKey: "caption") as? String
completionHandler(name, caption)
}else{
print(error?.localizedDescription ?? String())
completionHandler(nil, nil)
}
}
//now we can detect what object we have and show correct cell depending on object's type
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let object = tableObjects[indexPath.row]
//making downcast or if you won't use subclasses, then check type variable using switch case as I made in loadNews()
if let animal = object as? Animal,
let cell = tableView.dequeueReusableCell(withIdentifier: "AnimalCell") as? AnimalCell {
cell.captionLabel.text = animal.caption
//do additional stuff for the animal cell
//return cell
return cell
}
if let human = object as? Human,
let cell = tableView.dequeueReusableCell(withIdentifier: "HumanCell") as? HumanCell {
cell.captionLabel.text = human.caption
//do stuff with HumanCell
//return cell
return cell
}
if let element = object as? Element,
let cell = tableView.dequeueReusableCell(withIdentifier: "ElementCell") as? ElementCell {
cell.captionLabel.text = element.caption
//do stuff with ElementCell
//return cell
return cell
}
return UITableViewCell()
}
}
one simple solution, assign the data source of table view when your news loaded.. you can do that at the end of loadNews method
tableView.dataSource = self
Make sure the datasource was not assigned somewhere else like storyboard
I have a query that gets objects from the server I'm then reducing the number of objects by matching "packName" to "className" which should just give me the children of "packName".
from this i am populating an array of struct items and pulling out the data for the first index of the array.
this is fine but I'm just a bit concerned that if the number of children increases this may slow processing down. so i was wondering if there was a way to just retrieve the first item of the for loop, which is all I'm after as the query has been sorted in ascending order.
this is the function code below.
class func createHistory(packName: String, completeBlock: ((Bool) -> Void)? = nil) {
struct initialDataStruct {
var packNameStruct : String
var packIdStruct : String
var partNameStruct : String
var partIdStruct : String
var partIndexStruct : Int
}
var initialDataArray = [initialDataStruct]()
let historyClass = PFObject(className: packName)
let query = PFQuery(className: "Part")
query.includeKey("fromPack")
query.order(byAscending: "partName")
query.fromLocalDatastore()
query.findObjectsInBackground { (objects, error) in
if error != nil {
print(error!)
}
else if let parts = objects {
for object in parts {
// if the fromPack column has data
if let fromPack = object.object(forKey: "fromPack") as? PFObject {
// create the class name from the pack name
if let className = (fromPack.object(forKey: "packName") as? String) {
// packName was sent from JVC
// this will limit array items to how ever many children packName has
if packName == className {
// because its sorted could probably just get the first item here
let packName = fromPack.object(forKey: "packName") as! String
let packId = fromPack.objectId as String!
let partName = object.object(forKey: "partName") as! String
let partId = object.objectId as String!
let partIndex = 0
initialDataArray.append(initialDataStruct(packNameStruct: packName,
packIdStruct: packId!,
partNameStruct: partName,
partIdStruct: partId!,
partIndexStruct: partIndex))
}
}
}
} // for
historyClass.add(initialDataArray[0].packNameStruct, forKey: "packName")
historyClass.add(initialDataArray[0].partIdStruct, forKey: "packId")
historyClass.add(initialDataArray[0].partNameStruct, forKey: "partName")
historyClass.add(initialDataArray[0].partIndexStruct, forKey: "partIndex")
print(historyClass)
PFObject.pinAll(inBackground: [historyClass])
}
} // query
}
hello I have implemented an auto complete search on my app. I have cities stored in my mysql database and in app when user types any character or word, the app fetches result from the database and shows it. Now there is a small programming problem I am having and I don't know how to solve it.
The problem is in the same Array in which I am getting a City, I am getting country name and state name as well. As I have implemented a search only on cities not on state and country, I actually need the other columns(state,country) of those rows which are displaying based on user search city. I'll paste the code here for better understanding
class CityTableViewController: UITableViewController, UISearchResultsUpdating {
var dict = NSDictionary()
var filterTableData = [String]()
var resultSearchController = UISearchController()
var newTableData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.resultSearchController.active){
return self.filterTableData.count
}else {
return dict.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell
if(self.resultSearchController.active){
cell.cityNameLabel.text = filterTableData[indexPath.row]
cell.countryNameLabel.text = get the country name
cell.stateNameLabel.text = get stateName
return cell
}else{
cell.cityNameLabel.text = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString)! as String
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterTableData.removeAll(keepCapacity: false)
let searchWord = searchController.searchBar.text!
getCityNamesFromServer(searchWord)
let searchPredict = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
print("searchPredict is \(searchController.searchBar.text!)")
for var i = 0; i < self.dict.count; i++ {
let cityName = (((self.dict["\(i)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString)! as String
let countryName = (((self.dict["\(i)"] as?NSDictionary)!["Country"] as?NSDictionary)!["name"] as?NSString)! as String
let stateName = (((self.dict["\(i)"] as?NSDictionary)!["State"] as?NSDictionary)!["name"] as?NSString)! as String
newTableData.append(cityname)
}
let array = (newTableData as NSArray).filteredArrayUsingPredicate(searchPredict)
print("array is\(array)")
filterTableData = array as! [String]
self.tableView.reloadData()
}
func getCityNamesFromServer(searchWord:String){
let url:String = "http://localhost/"
let params = ["city":searchWord]
ServerRequest.postToServer(url, params: params) { result, error in
if let result = result {
print(result)
self.dict = result
}
}
}
}
If I try to setup new array of state and country then data doesn't shows up correctly. cities don't belong to his own state shows up. So How I can keep the order correctly.
Array:
dict
0 = {
City = {
code = 10430;
"country_id" = 244;
id = 8932;
name = Laudium;
"state_id" = 4381;
"updated_at" = "<null>";
};
Country = {
id = 244;
name = "South Africa";
};
State = {
"country_id" = 244;
id = 4381;
name = Gauteng;
};
}; etc
newTableData
["Lynnwood", "Lyndhurst", "Laudium"] etc
filterTableData
["Laudium", "La Lucia", "Lansdowne"] etc
You should search the dictionary for the matches and store the matched keys in an array and reference to these keys in the results.
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchWord = searchController.searchBar.text!
getCityNamesFromServer(searchWord)
self.filteredKeys.removeAll()
for (key, value) in self.dict {
let valueContainsSearchWord: Bool = (((value as? NSDictionary)?["City"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsSearchWord {
self.filteredKeys.append(key as! String)
}
}
self.tableView.reloadData()
}
Fill the tableview with this filtered keys:
let key = self.filteredKeys[indexPath.row]
let dictionary = self.dict[key] as! NSDictionary
cell.cityNameLabel.text = ((dictionary["City"] as? NSDictionary)!["name"] as? NSString)! as String
cell.countryNameLabel.text = ((dictionary["Country"] as? NSDictionary)!["name"] as? NSString)! as String
cell.stateNameLabel.text = ((dictionary["State"] as? NSDictionary)!["name"] as? NSString)! as String
return cell
Just save this filtered dictionary (self.filteredDictionary) and use that to populate the tableView.
I think the other problem is, when you call the server's search method (getCityNamesFromServer:) from updateSearchResultsForSearchController: the response from the server comes asynchronously and the process afterwards is using the old dictionary data, because the new one is not ready at the time of the processing.
You should try modifying the getCityNamesFromServer: method with a block completion like this:
func updateSearchResultsForSearchController(searchController: UISearchController) {
// Get search word
getCityNamesFromServer(searchWord) { () -> Void in
// Rest of the code comes here
}
}
func getCityNamesFromServer(searchWord:String, completionHandler: (() -> Void) ) {
let url:String = "http://localhost/"
let params = ["city":searchWord]
ServerRequest.postToServer(url, params: params) { result, error in
if let result = result {
print(result)
self.dict = result
}
completionHandler()
}
}
On Parse I have users with Facebook profile and Email login profile. So I want to bury for users data in my twitter-like app.
In my "messages" class on Parse I have column "sender" that contains pointers to parse users.
I just want to retrive and show the name of users in class "messages" contained in the column "sender" wich contains pointers to PFUsers of which I need data for keys
"first_name"
"last_name"
"profile_picture"
How can I retrive their data like name and image in order to show them in a tableview?
these are the declarations of my arrays:
var sendersArray : [String] = []
var picturesArray : [NSData] = []
maybe I could use something like this tuple, but I can't understand how to grab data from pointers
for user in list {
let firstName = "fist_name"
let lastName = "last_name"
let oProfileImage = NSData() //"image_profile" as! NSData
otherUsers.append((oName: firstName, oLastName: lastName, oImageProfle: oProfileImage))
}
version - 1:
I started with printing the whole pf object
//******************************************************
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.sender!
print(object)
print(theName)
sendersArray.append(theName)
let profilePicture = object["profile_pic"] as! PFFile
picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
//*******************************************************
version - 2:
then, found this solution, but still, doesn't
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages" )
theSearchQuery.includeKey("sender")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.sender!["first_name"] as? String
print(object)
print(theName)
sendersArray.append(theName)
let profilePicture = object["profile_pic"] as! PFFile
picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
errors:
seems to be a problem with sender, maybe I shouldn't use it
thanks in advance
let theName = object.objectForKey("sender")!.objectForKey("first_name") as! String
Complete Code:
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages")
theSearchQuery.includeKey("sender")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.objectForKey("sender")!.objectForKey("first_name") as! String
print(object)
print(theName)
self.sendersArray.append(theName)
let profilePicture = object["profile_picture"] as! PFFile
self.picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
Also, your picturesArray should be of type PFFile, like this:
var picturesArray = [PFFile]()
NOT NSData. change that at the top of your class.
-----EDIT------:
If you want to retrieve an image from a parse query, do this:
1) at the top of your class, declare the following arrays to store the results:
// your images will be stored in the file array
var fileArray = [PFFile]()
// your first and last names will be stored in String Arrays:
var firstNameArray = [String]()
var lastNameArray = [String]()
2) perform the query:
let query1 = PFQuery(className: "_User")
query1.orderByDescending("createdAt")
query1.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
if error == nil {
for x in objects! {
let firstName = x.objectForKey("first_name") as! String
let lastName = x.objectForKey("last_name") as! String
self.firstNameArray.append(firstName)
self.lastNameArray.append(lastName)
if x.objectForKey("profile_picture") as? PFFile == nil {
print("do nothing cause it's nil")
}
else {
let file:PFFile = x.objectForKey("profile_image") as! PFFile
self.fileArray.append(file)
}
}
self.tableView.reloadData()
}
})
Note I am using Swift 2 and Xcode 7. Syntax is slightly different in Xcode 6.4 and Swift 1.2.
I cannot get the createdAt value from Parse for each object.And I dont want to have to save an additional timestamp as a String when the data is sitting right there.
This is the list of what i was doing.I hope someone who can help
----------------------------------------------------------------------
var followArray = [String]()
var resultsNameArray = [String]()
var resultsIcon = [PFFile]()
var resultsImgArray = [PFFile?]()
var resultsTimeTampArray = [NSDate?]()
-------------------------------------------------------------------
func refreshResult()
{
var followQuery = PFQuery(className: "Follow")
followQuery.whereKey("user", equalTo: PFUser.currentUser()!.username!)
followQuery.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil
{
for object in objects!
{ self.followArray.append(object.objectForKey("Following") as! String)
}
var query = PFQuery(className: "Moments")
query.whereKey("userName", containedIn: self.followArray)
query.addDescendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil
{
for object in objects!
{
self.resultsNameArray.append(object.objectForKey("profileName") as! String)
self.resultsIcon.append(object.objectForKey("icon") as! PFFile)
self.resultsImgArray.append(object.objectForKey("image") as? PFFile)
//tried to use "createdAt" property but it i still getting nil value from Parse
self.resultsTimeTampArray.append(object.objectForKey("createdAt") as! NSDate)
}
//reload table
self.resultsTable.reloadData()
}
}
}
}
-----------------------------------------------------------------------
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:TimelineCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TimelineCell
enter code here //I'v got an error here!
//fatal error: unexpectedly found nil while unwrapping an Optional value
// dataFormatter
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var strDate = dateFormatter.stringFromDate(self.resultsTimeTampArray[indexPath.row]!)
return cell
}
Don't access it using objectForKey, just access it directly via the createdAt property.
As explicitly stated in the docs, the keys:
This does not include createdAt, updatedAt, authData, or objectId. It does include things like username and ACL.
You can access it directly in your for object loop like this.
object.updatedAt
instead of
objectforkey("updatedAt")