Set PFQueryTableViewCell imageView as a local image - ios

[Developing for iOS using Swift]
I am pulling data from Parse and populating a PFQueryTableView with it, and based on one of the values of the PFObjects that I am using to populate the tableView, I would like to set the imageView of the cell to an image stored locally in my Xcode images.xcassets.
This is my code:
// Set cells for each row of table
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
}
// Set image as read/unread
if let readStatus = object!["readStatus"] as? String {
if readStatus == "unread" {
cell.imageView?.image = UIImage(named: "unreadImage")
} else if readStatus == "read" {
cell.imageView?.image = UIImage(named: "readImage")
}
}
return cell
}
But it does not set the image...
If I change this line with a ! after imageView (like so): cell.imageView!.image = UIImage(named: "readImage")
I get the error:
fatal error: unexpectedly found nil while unwrapping an Optional value
So how can I set the cell's image based on a Parse value (but I want the image to be a local image)

Something is wrong with your cell object. First of all, you don't need the ! mark at the end. You should have it like this:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell
In Xcode 6.3 and Swift 1.2 your next line would not compile because you are checking for nil something that cannot be nil - you are force unwrapping the cell so it is no more an optional and you don't need to check for nil.
So remove the if cell == nil lines and see if your cell is set up correctly in IB (check if you've set the identifier).

Related

Cells in table view not being displayed

I am using a UITableView in one of my view controllers and am using a custom class called ListCell. The table view and the cells are being displayed, but the content in the cells are not being displayed, so the cell is empty. But if I press the cell it performs its task. So the content is there but its not being displayed. I think it has something to do with how I registered the class to the custom type ListCell. Here is how I declared it in viewDidLoad...
self.tblListView.registerClass(ListCell.self, forCellReuseIdentifier: "Cell")
Is there something wrong with the above line. And what it is causing the cell content to not being displayed?
Also I tried enabling size classes and that did not work. Also all the cells are being created programmatically.
The below line of code is responsible for displaying the cell data.
cell.displayAlertData(mutArrAlertList.objectAtIndex(indexPath.row) as! [NSObject : AnyObject])
the thing is mutArrAlertList's object at the first index is an optional type Optional(Temperature). I think the only way this will work if it is not optional. The object is being passed my a push notification, and what the push notification sends is not optional. So somewhere it randomly turns into an optional type. Is there a way I can get rid of that optional value around it?
Here is cell for row method:
func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier: String = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ListCell
//if cell == nil
//{
//let nib: NSArray = NSBundle.mainBundle().loadNibNamed("ListCell", owner: self, options: nil)
//cell = (nib.objectAtIndex(0) as? ListCell)!
//}
cell.selectionStyle = UITableViewCellSelectionStyle.None
print("")
print("This is cell: \(mutArrAlertList.objectAtIndex(indexPath.row) as! [NSObject : AnyObject])") //did this to check what the value is and it is an optional
cell.displayAlertData(mutArrAlertList.objectAtIndex(indexPath.row) as! [NSObject : AnyObject])
if indexPath.row == mutArrAlertList.count-1
{
if ApplicationDelegate.intPage > mutArrAlertList.count
{
//do nothing
}
else
{
ApplicationDelegate.intPage = ApplicationDelegate.intPage + 10
self.reloadDataFromdb()
}
}
return cell
}

cell.imageView returns nil

I am using Pars in this project and specially PFQueryTableViewController which subclasses PFTableViewCell instead of UITableViewCell.
cell.textLabeland cell.detailTextLabelare set no problem but cell.imageView returns nil. Why?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PFTableViewCell
if let podcast = object {
if let name = podcast["name"] as? String {
cell.textLabel!.text = name
}
if let artist = podcast["artist"] as? String{
cell.detailTextLabel!.text = artist
}
if let artwork = podcast["artwork"] as? PFFile {
print(cell.imageView)
// cell.imageView!.file = artwork
// cell.imageView?.loadInBackground()
print("we have the artwork \(artwork)")
} else {
cell.imageView?.image = UIImage(named: "place")
print("using placeholder image")
}
}
return cell
}
So first, do you mean imageView is nil or do you mean imageView.image is nil? If your imageView is nil, that means the whole imageView is not being instantiated. How are you creating this imageView? Are you using a storyboard or a nib or are you creating the imageView programmatically? If you are doing it programmatically, make sure you are calling the init method on UIImageView before you try setting its .image property.
If the problem is that the .image property on your UIImageView is nil, then perhaps you have added your image incorrectly to your Images.xcassets folder or you have slightly misspelled the name of the image.
If the problem is you can't see the image, the perhaps if you have created the UIImageView programmatically, you haven't added it to your viewController's view heirarchy. This can be done with
self.view.addSubview(imageView)
So first narrow these things down, and if you come back with more information, then it will give me a better idea of the issue.

Converting Objective to Swift Table view not displaying the data in the cells

I'm still very inexperienced with Swift and am having a problem converting an objective-c based app.
Most of the app is working ... including changing size and colors of section headers and background color of the cells but I cannot display the content of the cells (a TextView and a switch).
Any suggestions about fixing this would be appreciated.
I'm including the code where I change the background color which is where I suspect the problem resides:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//variable type is inferred
var index = indexPath.row
NSLog ("index %d",indexPath)
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
//we know that cell is not empty now so we use ! to force unwrapping
var grayishCyan = colorWithHexString("#9bc2c2")
var grayishRed = colorWithHexString("#ffc2c2")
if (indexPath.row == 0 || indexPath.row%2 == 0) {
cell!.backgroundColor = grayishCyan;
}
else {
cell!.backgroundColor = grayishRed
}
return cell!
} // end of cellForRowAtIndexPath
I removed the cellForRowAtIndexPath function and replaced it with the willDisplayCell function

Can't set PFTableViewCell imageView?

I'm trying to display an icon for each PFTableViewCell based on the imageView property which doesn't work. The code I'm using is below which throws the follow error, "fatal error: unexpectedly found nil while unwrapping an Optional value".
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! i!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
cell.textLabel?.text = "Some Label"
cell.detailTextLabel?.text = "Another label"
cell.imageView.image = UIImage(named: "icon.png")
return cell
}
I had the same issue. Your crash is linked to the imageView being nil.
I guess this is a bug with PFTableViewCell: it does not "see" the standard imageView from the "Basic" style, and cannot create the corresponding PFImageView (I opened an issue with ParseUI).
Generally speaking, it's better to set your line like so:
cell.imageview?.image
then you won't have the crash.
You won't have the image either, but that's because of Parse's PFTableViewCell ...
If you want your code to work, you need to create a custom cell, with an imageView that you need to set as a PFImageView. This way, there will indeed be an imageView, and the whole PFTableViewCell file loading works perfectly.

Overriding UITableView in Parse with Swift

All,
In swift while using Parse as my backend, I have created a class which inherits from PFQueryTableViewController. I see my data going into a tableview - thats fine.
I am trying to customise my cells a bit, and I overriding CellForRowAtIndexPath - like below :
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell!
{
var cellIdentifier = "eventCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? PFTableViewCell
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
}
// configure cell
var CellTitle = object.objectForKey("Title") as String
cell?.textLabel = CellTitle
}
}
As the object using comes back as [AnyObject] in swift, I have created a variable and I have casted it to a string. And then I am trying to show that string in Cell.textlabel.
I am getting the error : Cannot assign to the result of this expression.
Can anyone please show me the right direction on this.
I think the problem is that you're attempting to assign a String directly to cell?.textLabel UILabel. Instead try changing this line:
cell?.textLabel = CellTitle
to this
cell.textLabel?.text = CellTitle
so you're setting the text property of the UILabel instead.

Resources