Delegate and Datasource for second Collection View - ios

There is a second UIcollectionView in my app, which is having an imageview.
But every-time I get an error like this
Thread 1: "could not dequeue a view of kind: UICollectionElementKindCell with identifier SliderCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard"
Where SliderCell is the Id of first UICollectionview
I know that this is because the identifier is not registered / misspelled. and I tried registering. But the same error happens.
What I found is when I add the delegate and Datasource of the second CollectionView to the same View, this error happens.
is there any solution for this?

[Solved]
Before :
first I declared dequeueReusableCell method for both cells, set the cell values and put the return statement in an if-else condition.
After:
all three lines for first collectionView in if and declared other in else

Related

Unable to dequeue a cell with identifier while using a custom UITableIView

In my setup, I have 3 tableViews (nested) - tableView inside a tableView cell inside a tableVIew cell.
My goal is to find out touch events on the “mid-level” tableView. When a mid-level tableView is touched, I would like to extend its and its parent tableView height to predetermined values.
I created a custom UITableView with a second tag property (“secondTag”) and inside the tableView methods, I cast tableView to CustomTableView. So, inside tableView didSelectRowAt I'll had two indexes - IndexPath and secondTag. The first one was theIndexPath of the mid-level tableView and the second one was the custom UITableView tag that used for the indexing of the “parent” tableView.
But despite setting the cell identifier properly (inside the storyboard) I got the following error: "unable to dequeue a cell with identifier middleCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard".
One more note: inside tableView didSelectRowAt I used reloadData() on both the mid-level tableView cell and its parent tableView cell, since I needed both of them (sub/parent tableView cells) to change their height after a touch has occurred.
I think the double reloading may be the cause of my problems but I must reload both the parent and its nested “mid-level” tableView since I need both their heights to change after a touch was made.
*This question comes in to continue the following one: Need help adding new stored property to UITableView

Difference between registerClass and registerNib UICollectionView

I have a UICollectionView which shows 3 different types of custom cells. Each cell is defined in separate xib. Before i used to use registerClass to register cell class with the UICollectionView. Inside initialiser of cell, i used to load view from nib file. With this approach few random crashes were recorded when it went to dequeue cell in cellForItemAtIndexPath and exception was
Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named (null)'
To be precise above exception occurred when it tried to load cell from the nib. Now i replaced registerClass with registerNib, to register nib file again cell identifier for UICollectionView and remove nib loading code from cell initialiser. Since then above crash hasn't been reported again.
May i know what is actually difference in way registerClass and registerNib? Also what is the best way of specifying cells for UICollectionView?
If the cell class was written in code, use the registerClass: method.
In the event that the cell is contained within an Interface Builder NIB file, the registerNib: method is used instead.

Unable to access custom table cell variables

I'm using Xcode 6.0.1, with Swift. I have a Table View which was working fine for a normal type of cell, but it's started displaying errors after I changed it to use a custom cell. I made a Table View Cell in the storyboard, made a Cocoa Touch Class file, MyCustomCell, which is a subclass of UITableViewCell, and set the custom cell in the storyboard to use this class, in the Identity inspector. I wired a text field I put into the custom cell in the storyboard to the MyCustomCell.swift file as a variable called someData.
The problem occurs here:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as MyCustomCell
cell.someData!.text = "testing"
return cell
}
Come runtime, the "testing" line gives me a fatal error: unexpectedly found nil while unwrapping an Optional value error, assuming that I manually registered the cell class in viewDidLoad, like this:
tableView.registerClass(MyCustomCell.self, forCellReuseIdentifier: "Cell")
If, instead, I never register the class, instead putting "Cell" in the storyboard as the Restoration ID for the custom cell, I get the same exact error nil error message, but on the "let cell =" line instead of the "testing" line.
The MyCustomCell class wires the someData variable and has init, awakeFromNib, and setSelected shells but nothing else.
I'm not really sure how to fix this. Maybe I need to create a nib for the custom cell or something? Any help would be appreciated.
(By the way, I'm using Core Data to store entities for each table cell, but that's not shown here, since I'm only concerned with getting custom cells working right now.)
You are using the Restoration Id (Identity Inspector) instead of the Indentifier (Attributes Inspector) in Interface Builder.
When UITableView is instantiated from storyboard calling
registerClass(_:forCellReuseIdentifier:) causes problems. You need to give reueseIdentifier in storyboard in attributes inspector.

Can't select UICollectionViewCell in the xib file

I am doing a UICollectionView project but I didn't use storyboard to manage the views. I subclassed a UICollectionViewController in MyCollectionViewController with a MyCollectionViewController.xib file created. In the xib file, I dragged a Collection View onto the view, as the figure shown.
I believe that the grey boxes are the Collection View Cells and I am supposed to click on them and change their Identifier so that these two lines of code recognize this is the cell.
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"CollectionViewCell"];
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"CollectionViewCell " forIndexPath:indexPath];
However, I couldn't click on the cells neither can I drag a new Collection View Cell onto the Collection View. I am getting the error message like this:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Can someone help me?
I figured it out.
The reason was I have used duplicated cell identifier: #"CollectionViewCell".
Change it and everything is working perfectly!
So, be aware of the identifiers! Make sure that each one is unique and don't even mess up with class names. Hopefully this can help out some people who have the same problem as me.

Strange error when adding items to prototype cells in storyboard-IB

I have quite a large project (~20 scenes). One of which is a TableViewController with a custom UITableViewController class. I have given the cell a reuse identifier, and added a label to it. When I try and Ctrl+Drag the label to the UITableViewController header file to create an outlet, I get the following error:
error: Illegal Configuration: Connection "tableInfoView" cannot have a
prototype object as its destination.
What is this? Am I overlooking something obvious? Or do I need to create a custom cell class and drag the outlet to that? If so, how do I then specify the data which is displayed uniquely for each cell from the UITableViewController?
In fact you can't just make an outlet from a dynamic cell prototype in the UITableView delegate view controller.
You'll have to subclass UITableViewCell and then attribute this class to your prototype.
Then you can Ctrl-Drag from the Label to the UITableViewCell subclass header file.
Finaly you can access to this outlet in the delegate code after having imported the UITableViewCell header file in it.
This is documented by Apple there at "The Technique for Dynamic Row Content" section.
or you could give the label a tag (e.g. 100) and use
myLabel = [myTableView viewForTag:100];
to get the label
I had the same error myself. Just to add one more potantial root cause for future readers:
In my case I copied a control (a Button in this case) from one prototype cell to the next and the action still referred to the neighbor cell. My table has several different prototype cells.
The fact, that it acutally was a proper subclass of UITableViewCell which was properly connected to the prototype cell made it difficult to actually see the mistake.
Tag the label and you can reach the label anywhere in the viewcontroller like with viewWithTag from the table view.
UILabel *destinationLabel = (UILabel *)[self.tableView viewWithTag:1];
destinationLabel.text = #"Label Destaination";
I faced the same problem but later it turned out that it was just a silly mistake.
I mistakenly dragged the label from Cell to my controller's #interface
This could be your problem too. just cross check once.
Set the right reuse identifier used in .m file in the Storyboard for the Prototype cell.I had the same situation and this helped me
After doing every thing right if problem still exist then just removed all outlets and rejoin them carefully and it worked very fine for me.

Resources