Core-Plot inside a UICollectionViewCell - ios

I have a UICollectionView that has a Core-Plot graph inside of one of the cells.
I have no issues when making a Core-Plot graph in a UIView, but I'm having troubles getting this Core-Plot graph to work inside a UICollectionViewCell.
In MyCollectionViewCell.swift
import UIKit
import CorePlot
class MyCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var corePlot: CPTGraphHostingView!
}
In MyViewController.swift
import UIKit
import CorePlot
class MyViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let theCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath) as! MyCollectionViewCell
let graph = CPTXYGraph(frame: theCell.corePlot.bounds)
theCell.corePlot.hostedGraph = graph // This is where it crashes
...
}
...
}
extension MyViewController: CPTScatterPlotDataSource, CPTScatterPlotDelegate {
...
}
-[UIView setHostedGraph:]: unrecognized selector sent to instance 0x7f8a9aa20240
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setHostedGraph:]: unrecognized selector sent to instance 0x7f8a9aa20240'

You need to set class name CPTGraphHostingView to corePlot
#IBOutlet weak var corePlot: CPTGraphHostingView!
Inside the xib file / prototype of cell in IB , here UIView in crash
[UIView setHostedGraph:]
means that you didn't assign it

Related

How to proper create a custom cell with a xib file, register, and load it ?

I want
to create a custom table cell and add all the components and register it
I did
I created these files below :
PlaceDetailCell.xib
with the id of customPlaceDetailCell
CustomPlaceDetailCell.swift
import UIKit
class CustomPlaceDetailCell: UITableViewCell {
#IBOutlet weak var country: UILabel!
#IBOutlet weak var address: UILabel!
#IBOutlet weak var distance: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I register
//register xib file
placesTable.register(UINib(nibName: "PlaceDetailCell", bundle: nil), forCellReuseIdentifier: "customPlaceDetailCell")
This is my cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customPlaceDetailCell", for: indexPath)
as! CustomPlaceDetailCell
if places[indexPath.row]["name"] != nil {
cell.textLabel?.text = places[indexPath.row]["name"]! + " (" + distances[indexPath.row] + " miles)"
}
return cell
}
I got
a crash when I click on that cell even if the app is successfully Build.
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key address.'
*** First throw call stack:
I hope
someone can provide some guidance for me to do the right things.
You need to set the xib file custom class to the class you cast it to
let cell = tableView.dequeueReusableCell(withIdentifier: "customPlaceDetailCell", for: indexPath)
as! CustomPlaceDetailCell
as it seems that xib name is PlaceDetailCell with class PlaceDetailCell and you cast it to CustomPlaceDetailCell
if you register this
placesTable.register(UINib(nibName: "PlaceDetailCell", bundle: nil), forCellReuseIdentifier: "customPlaceDetailCell")
then the xib custom class should be set to CustomPlaceDetailCell not to PlaceDetailCell which is expected as always the xib name = it's class name that you create and then wanted to change the class name

unable to dequeue a cell with identifier cell with properly defined identifier

I experienced weird issue with TableViewController
*** Assertion failure in -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3694.4.18/UITableView.m:7732
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
It makes no sense while
Identifier is set properly.
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
However if i place
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
in ViewDidLoad, then it works. However prototype from storyboard will not and is not used.
At this point i have no idea what's going on.
TableViewController class is set also correctly.
I understood your question, There are only one reason that your tableview can not find the registered cell.
Solution is that you have to check your code one more time. Please check your identifier spelling in both storyboard and cellForRowAt method.
if this solution not work than follow this steps:
paste below code in cellForRowAt method
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
Delete all cells from tableviewcontroller from storyboard.
Drag and drop new TableViewCell and give proper indetifer "cell"
Build and run.
This solution will definitely work.
As you using a TableViewCell Differently So I am sure you had also created a tableCell Class , as in my case its "CustomTableCellClass" as follow
class CustomTableCellClass: UITableViewCell {
weak var CategoriesDelegate: CategoriesVcDelegate?
weak var itemsDelegate: ItemsvcDelegate?
//MARK: Outlets of CategoriesVcTableView
#IBOutlet weak var ProductImage: UIImageView!
#IBOutlet weak var ProductName: UILabel!
#IBOutlet weak var ProductPrice: UILabel!
#IBOutlet weak var ProductStarImage: UIButton!
}
My cellReuseIdentifier Declaration -
let cellReuseIdentifier = "cell"
Now Deque it as
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.CategoriesTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CustomTableCellClass
cell.CategoriesDelegate = self
cell.ProductStarImage.tag = indexPath.row
cell.ProductName.text = productNameArray[indexPath.row]
cell.ProductPrice.text = productPriceArray[indexPath.row]
cell.ProductImage.image = productImagesArray[indexPath.row]
return cell
}
Hope it helps :)
It seems that issue was in custom library i was using for navigation. Instead initialising view controller from storyboard identifier, it was initialising class directly, thus storyboard view controller was useless.
Everything works well now.

appname.ViewsVC collectionView:cellForItemAtIndexPath:]: unrecognized selector sent to instance

I am building a collection view and am getting the following error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[appname.ViewsVC collectionView:cellForItemAtIndexPath:]: unrecognized selector sent to instance
Here is the code where i custom cell
// cell configuration
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ViewsCell", for: indexPath) as! ViewsCell
cell.fullnameLbl.text = self.viewsArray[(indexPath as NSIndexPath).row].capitalizeEachWord
// pic setup
avaArray[indexPath.row].getDataInBackground { (data:Data?, error:Error?) in
if error == nil {
cell.avaImg.image = UIImage(data: data!)
}
}
return cell
}
I created the collection view and the cell in storyboard and have given them the proper identifiers. I also connected the UIimageView and the label to the cell class.
Any ideas what could be wrong.
I realized that I was missing UICollectionViewDataSource and UICollectionViewDelegate at the top of the View controller
class ViewsVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

Terminating app: could not dequeue a view of kind: UICollectionElementKindCell with identifier

I'm implementing a custom .xib for a UICollectionViewCell and the app is crashing with:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier DaySelectorCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
There are several answers around that suggest that the identifier has a space in it or is missing. I've triple-checked everything though, and it's still throwing.
Here's my Storyboard config for the xib:
Here's my DaySelectorCell.swift class:
import Foundation
import UIKit
class DaySelectorCell: UICollectionViewCell {
#IBOutlet weak var dayOfWeekButton: UIButton!
var dayNumber: Int = -1
}
And my collectionView delegate implementation:
extension CustomViewController: UICollectionViewDelegate, UICollectionViewDataSource {
let days = ["M", "T", "W", "T", "F", "S", "S"]
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let id = "DaySelectorCell"
var cell: DaySelectorCell! = self.daySelector.dequeueReusableCellWithReuseIdentifier(id, forIndexPath: indexPath) as? DaySelectorCell
if cell == nil {
collectionView.registerNib(UINib(nibName: id, bundle: nil), forCellWithReuseIdentifier: id)
cell = collectionView.dequeueReusableCellWithReuseIdentifier(id, forIndexPath: indexPath) as? DaySelectorCell
}
cell.dayOfWeekButton.setTitle(days[indexPath.row], forState: .Normal)
cell.dayNumber = indexPath.row
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
}
If dan's answer doesn't work (and you already have registered the cell), do a clean, remove Xcode's build folder manually, quit Xcode, reboot the Mac and try again. Apple has done a very bad job with Xcode...

Load UITableViewCell from xib file in a UITableView in a view

I have a view (not a view controller) that pop up when I press a button.
In this view, I have a UIButton and a UITableView. I want to use the UITableViewCell I designed in a xib file.
So I created my cell class:
class PickerViewCell: UITableViewCell {
#IBOutlet weak var flagImageView: UIImageView!
#IBOutlet weak var codeLabel: UILabel!
#IBOutlet weak var nameLabel: UILabel!
func setCell(myObject: myObject) {
flagImageView.image = UIImage(named: myObject.code)
codeLabel.text = myObject.code
codeLabel.font = myObject.kPickerViewCodeFont
nameLabel.text = myObject.name
nameLabel.font = myObject.kPickerViewNameFont
}
}
My xib file:
Here I didn't set the owner of the xib (I tried with as well), I put the custom class for the UITableViewCell and no reuse identifier (I tried with as well).
In the init function of my view (the one containing the table view), I do the following:
let nib = UINib(nibName: "PickerViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
And finally I implement the method:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PickerViewCell
let myObject = myObjects[indexPath.row]
cell.setCell(myObject)
return cell
}
When I want to show (create) my view, the debugger stop at
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PickerViewCell
and I get the following error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7f9b934d8c70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key codeLabel.'
What's wrong?
EDIT:
Check your connection of #IBOutlet weak var codeLabel: UILabel! with IB in PickerViewCell. Try to reconnect it.

Resources