UICollectionView and Parse Images ( Swift ) No Error but Crash - swift

The program launched and displayed the images from parse with no error, when i ran it again it crashed. My question is if it is because the images aren't scaled to the correct size of the custom cell.
here is the error: the item width must be less than the width of the UICollectionView minus the section insets left and right values.
fatal error: Array index out of range
here is my function:
// Get Brands
func getBrand () {
// Create PFQuery
var query:PFQuery = PFQuery(className: "BrandInfo")
// Call findobjects
query.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]?, error: NSError?) -> Void in
// refresh array
self.brands = [String]()
self.pics = [UIImage]()
// loop through array
for brandObject in objects! {
// get PFObjects
let brandName:String? = (brandObject as! PFObject)["Name"] as? String
if brandName != nil {
self.brands.append(brandName!)
}
let brandPicture = brandObject["Image"] as! PFFile
brandPicture.getDataInBackgroundWithBlock({ (imageData:NSData?, error:NSError?) -> Void in
if(error == nil){
let brandImage = UIImage(data: imageData!)
self.pics.append(brandImage)
println(self.pics.count)
}
})
}
// Refresh CollectionView
self.collectionView.reloadData()
}
}
// Setting up collection view
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.brands.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:collectionViewCell = self.collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! collectionViewCell
// Get elements
let picToDisplay:UIImage? = pics[indexPath.row] // here is where the warning occurs
let dataToDisplay:String = brands[indexPath.row]
let imageView:UIImageView? = cell.viewWithTag(2) as? UIImageView
// Set labels
cell.brandImage.image = picToDisplay
cell.brandLabel.text = dataToDisplay
return cell

Check if you are getting same count for brands and pics array.
In your case you should append array only if you both the brandImage and BrandName.So that your pics and brands array count will be same.

Related

Unexpected Non Void return value in CollectionView cellForItemAtIndexPath Datasource method

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? PictureCellForHomeVC {
picArray[indexPath.row].getDataInBackgroundWithBlock({ (data: NSData?, error: NSError?) in
if error == nil {
if let data = data {
let image = UIImage(data: data)
cell.imageView.image = image
return cell
}
}
})
}else {
return UICollectionViewCell()
}
}
In the above code I receive the error:
Unexpected Non Void return value in void function
The picArray in my above code is code with PFFile objects which I can call the getDataInBackgroundWithBlock method.
I understand what the error means since the completionHandler of the getDataInBackgroundWithBlock returns void so I can't return my cell in it, but I am stuck on how to fix this problem.
I can't even pass a seperate closure to the method cellForItemAtIndexPath since I do not call the method and it is a datasource method. Does anyone know how I can fix this issue?
You're trying to return something that you think is returning within your function but is actually trying to return something from getDataInBackgroundWithBlock which is supposed to return void (which is not allowed hence the error).
You could do something like this which loads the image in the background while giving it a placeholder image in the meantime (for added effect):
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? PictureCellForHomeVC {
picArray[indexPath.row].getDataInBackgroundWithBlock({ (data: NSData?, error: NSError?) in
if error == nil {
self.loadImage(inImageView: cell.imageView, withData: data)
}
})
return cell
} else {
return UICollectionViewCell()
}
func loadImage(inImageView imageView: PFImageView, withData imageFile: PFFile?) {
imageView.image = UIImage(named: "placeholder.png") // Some placeholder while the image loads asyncronously
if let imageFile = imageFile {
imageView.file = imageFile
imageView.loadInBackground(nil)
}
}

Only one custom tableviewcell working correctly

I created a custom tableviewcell with a photo and two labels. I queried some data from parse and the cells are suppposed to update the image and labels to reflect the query, however only the first viewcell works correctly. The image and labels work, however the second viewcell only displays the image correctly, the uilabels do not display any text. I've looked over the code multiple times and cant seem to figure out what i am doing wrong...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mySpotCell")/*, forIndexPath: indexPath)*/as? CustomTableViewCell
// cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
if let value = mySpots[indexPath.row]["location"] {
let location = CLLocation(latitude: (value.latitude)!, longitude: (value.longitude)!)
self.geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemark, error) -> Void in
if error != nil {
print("error: \(error!.localizedDescription)")
}
if let pm: CLPlacemark = placemark![indexPath.row] {
// var pm = placemark![indexPath.row] as CLPlacemark
//self.parkingSpotAddress.text = pm.thoroughfare
// self.navigationController?.navigationBar.topItem?.title = pm.thoroughfare
//cell!.textLabel?.text = "\(pm.subThoroughfare!) \(pm.thoroughfare!)"
cell?.subtitleLabel.text = "\(pm.subThoroughfare!) \(pm.thoroughfare!)"
cell?.titleLabel.text = pm.description
print(cell?.subtitleLabel.text)
print(cell?.titleLabel.text)
}
})
if let parkingSpotImageFile: PFFile = mySpots[indexPath.row]["firstPhoto"] as! PFFile! {
parkingSpotImageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error == nil {
cell?.spotImageView.image = UIImage(data: imageData!)
// self.imageIndicator.stopAnimating()
// self.imageIndicator.hidden = true
}
})
}
I think you can check your func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int first. The value you return inside is the number of row it will show and load in tableview.
And my cellForRowAtIndex is like this, I think yours should be alright also. Just check the number of row in section.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:MenuTableViewCell = tbvMenu.dequeueReusableCellWithIdentifier("identifier") as! MenuTableViewCell
let temp:String = MenuArr[indexPath.row] as! String
cell.mainMenuTitle.text = temp
return cell
}

UITableView in swift

can anyone tell me what's wrong with my code i always get the same data repeated in all cells
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.textColor = UIColor.whiteColor()
let query = PFQuery(className:"book")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
var contentString:String = String()
if error == nil {
print("Success retrieved \(objects!.count) scores.")
if let objects = objects {
for object in objects {
let a:String = object.objectForKey("title") as! String
contentString = a
}
}
cell.textLabel!.text = contentString
}}
return cell
}
It will be better to query all your data into viewDidLoad() or any life cycle you would like.
var contentString = [String]() //<-- use an array of string instead of a string variable
override func viewDidLoad()
{
super.viewDidLoad()
let query = PFQuery(className:"book"){ query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
print("Success retrieved \(objects!.count) scores.")
if let objects = objects {
for object in objects
{
let value = object["title"] as! String
self.contentString.append(value) //<-- save the query value to your array
}
self.tableView.reloadData() //<--- then you reload your UI
}
}}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.textColor = UIColor.whiteColor()
cell.textLabel!.text = self.contentString[indexPath.row] //<-- then assign the value into your cell
return cell
}
content inside your loop should be like this.
let a:String = object.objectForKey("title") as! String
contentString += a
and i noticed your block calls for same query always. can you tell me whats the different there
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
var contentString:String = String()
if error == nil {
print("Success retrieved \(objects!.count) scores.")
if let objects = objects {
for object in objects {
let a:String = object.objectForKey("title") as! String
contentString = a
}
}
cell.textLabel!.text = contentString
}}
The query in cellForRowAtIndex is trouble. Imagine, every time a cell comes into view, the code asks for every book from the server.
Put the query outside the datasource method (viewWillAppear, for example). When the query is complete, initialize an array property with the results (e.g. self.bookObjects = objects).
Then in cellForRowAtIndex...
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.textColor = UIColor.whiteColor()
// this is the important idea...
let object = self.bookObjects[indexPath.row]
let a:String = object.objectForKey("title") as! String
cell.textLabel!.text = a
return cell

Unable to load and display data from parse

var findPublisher:PFQuery = PFUser.query()!
findPublisher.whereKey("objectId", equalTo: quote.objectForKey("publisher")!)
findPublisher.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFUser] {
for object in objects {
//println(object.objectId)
let user:PFuser = (objects as NSArray).lastObject as PFUser
cell.publisherLabel.text = user.username
}
}
}
// Configure the cell...
}
The code here is supposed to query for the objectId and display the username instead of the label. I am getting an error that says use of undeclared type of 'PFUser'... I used "println" right after the error == nil statement and it logs correctly so the error definitely occurs after there.
if you are using a TableViewController, you don't need to create an Outlet of the tableview. So you need to query all the objects from the _User class into an array. So it is up to you to know where you want to place this code either in the ViewDidLoad or ViewWillAppear.
var arrayOFUser :NSMutableArray = []
var findPublisher:PFQuery = PFQuery(className:"_User")
findPublisher.findObjectsInBackgroundWithBlock{ (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil{
if let objects = objects as? [PFObject] {
for object in objects {
var getusername = object["username"] as! String
self.arrayOFUser.addObject(getusername)
self.tableview.reloadData()
}
}
}
}
// return the count of user into the each cell in the tableview
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrayOFUser.count
}
then you go into the ** cellForRowAtIndexPath** method
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableview.dequeueReusableCellWithIdentifier("CellSubtitle", forIndexPath: indexPath) as! UITableViewCell
let name = self.arrayOFUser[indexPath.row] as! String
cell.textlabel?.text = name
return cell
}
QuickNote "CellSubtitle" is the name of the cell in the tableview so go into the storyboard in change it.

Swift: UICollectionView & UIImage fatal error: Array index out of range

Hi Im new to swift and need some help thank you. The program worked once, tried to run it again then it crashed. The items are in the array when I debugged but when I try to display the images I get the fatal error.
// Get Brands
func getBrand () {
// Create PFQuery
var query:PFQuery = PFQuery(className: "BrandInfo")
// Call findobjects
query.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]?, error: NSError?) -> Void in
// refresh array
self.brands = [String]()
self.pics = [UIImage]()
// loop through array
for brandObject in objects! {
// get PFObjects
let brandName:String? = (brandObject as! PFObject)["Name"] as? String
if brandName != nil {
self.brands.append(brandName!)
}
let brandPicture = brandObject["Image"] as! PFFile
brandPicture.getDataInBackgroundWithBlock({ (imageData:NSData?, error:NSError?) -> Void in
if(error == nil){
let brandImage = UIImage(data: imageData!)
self.pics.append(brandImage)
println(self.pics.count)
}
})
}
// Refresh CollectionView
self.collectionView.reloadData()
}
}
// Setting up collection view
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.brands.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:collectionViewCell = self.collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! collectionViewCell
// Get elements
let picToDisplay:UIImage? = pics[indexPath.row] // this is the error line
let dataToDisplay:String = brands[indexPath.row]
let imageView:UIImageView? = cell.viewWithTag(2) as? UIImageView
// Set labels
cell.brandImage.image = picToDisplay
cell.brandLabel.text = dataToDisplay
return cell
}
///////////////////////////////
struct brandCollection {
var brandText:[String] = [String]()
var brandImage:[UIImage?] = [UIImage]()
}
You have brands.count items, and you need brand.count images. But you are using a different array pics which will fail if it has less elements than brands. Looking at your code, there are several possibilities why this could happen.
This is bad design. You should have a data array that encapsulates all the information you need. Each image should be connected to its object (the brand), not held in some other array.