Reputation: 25
I have a list of users stored on Parse (backend), with a column called "profile_picture" that stores the user's profile picture. However, there are certain users without a profile picture, thus their profile_picture column is "undefined" (has no value).
I am using a searchbar to query through all the users and update the tableview with the user's profile pic and username. I do this by appending the username to var searchResults = String , and the profile pic to var searchImages = PFFile after the query like so:
let query = PFQuery.orQuery(withSubqueries: [usernameQuery!,fbUsername!, firstnameQuery!, lastnameQuery!]);
searchActive = true;
query.findObjectsInBackground { (objects, error) in
if error != nil {
print("There was an error getting userlist");
}
else {
if let users = objects {
self.searchResults.removeAll(keepingCapacity: false);
for object in users {
if let user = object.object(forKey: "username") as? String {
self.searchResults.append(user);
}
if let picture = object.object(forKey: "profile_picture") as? PFFile {
self.searchImages.append(picture);
}
self.searchTableView.reloadData();
}
The problem is that when the "profile_picture" column is empty, it does not append anything, which then means that the searchResults array (containing usernames) and the searchImages array (containing the PFFiles) have different sizes, which results in uncoordinated assignments of values. They are supposed to parallel arrays. And I'm using values in these arrays to assign values in a table cell.
Hope you guys understand my problem! Thanks in advance
Upvotes: 0
Views: 188
Reputation: 1239
So your username
field is definitely not empty. I think you can add an else
after you check whether profile_picture
is nil or not. Like:
if let picture = object.object(forKey: "profile_picture") as? PFFile {
self.searchImages.append(picture);
} else {
self.searchImages.append(UIImage(named:"placeholder"))
}
Upvotes: 0