Can't select UICollectionViewCell in the xib file - ios

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.

Related

Delegate and Datasource for second Collection View

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

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.

Adding xib in CollectionView

Problem such, downloaded and installed MWFeedParser, it worked all right, but that's a problem, I do not need that to show a list of the main xib news, and that he would need to show the menu that I create. But when you try to add a menu, ie create a new a new xib, I choose in the settings (general) in paragraph mainintarface, choose your new xib (MainNav) I get an error
libc++abi.dylib: terminating with uncaught exception of type NSException
Dig deeper into the net, I came across this response, I understand the problem is that I have not added my new xib in UICollectionView? I porobyval remove UIKit.framework, then got out unreal number of errors. Well, actually the question how do I add my new xib in UICollectionView?
You need register your xib cell to your UICollectionView on -(void)viewDidLoad of your view Controller.
[self.collectionView registerNib:[UINib nibWithNibName:#"nibName" bundle:nil]
forCellReuseIdentifier:#"cellIdentifer"];
Click [here] (https://github.com/lequysang/TestCollectionViewWithXIB")

invalid nib registered for identifier (CELLNAME) - nib must contain exactly one top level object which must be a UITableViewCell instance

Sorry for the long title, but I wanted it to be clearly seen from a google search.
Also, this differs from many of the other similar questions on here as its not specifying a 'null' identifier but one that actually exists.
Essentially the problem occurs when your trying to navigate using one of the cells in a table view controller to segue to another view (Whatever that view may be).
Most people would run into this problem after gunning through the apple tutorial on the 'To-Do List' and expecting the cells to operate in the same motion regardless of their purpose.
This problem is probably simplistic to most but for a beginner, its quite hard, it took me around 3 hours.
Basically the error is:
invalid nib registered for identifier (prototypeCell) - nib must contain exactly one top level object which must be a UITableViewCell instance
Where 'prototypeCell' would be whatever your cell is called. This is an exception that occurs immediately as the app is launched.
I had the same problem as above but I wasn't using storyboards and the problem just appeared out of the blue.
I found that the solution was in the tableview cell file. I had added a uibutton, but it had been added outside the bounds of the cell by mistake. This meant it was almost like an extra view in the uiview.
Once I found it and deleted this extra view the problem disappeared immediately.
If you have having this error, check the uitableviewcell for extra views and objects added by mistake
The answer of simon_smiley pointed me to the right direction, but some more trial-and-error was needed leading to the following solution:
The problem doesn't only occur for additional top-level UIView objects, but also for gesture recognizers. So make sure not to use any gesture recognizers in your failing XIB file, you can set them up in code instead.
For example you can do it in awakeFromNib as noted by vahotm in the accepted answers comments.
I had the same problem! And in my case custom cell was the subclass of UIView by mistake instead of UITableViewCell. So replacing UIView with UITableViewCell fixed the problem!
Same problem because I drag and drop a UITapGestureRecognizer on the subviews of ContentView. Just remove it.
In my case, I had an extra ImageView inside the xib added by mistake. Removed it and everything worked perfectly.
The problem is that there are multiple cells in your storyboard that have the same name. Such as for a single table view, there are multiple cells that have the same identifier. In my case I had three cells that were all called 'prototypeCell'.
The fix is actually quite easy. For every cell, name it a simple name with the cell number at the end. This cell number has to match the indexPath.row later on, so basically start from 0 onwards.
For Example:
prototypeCell0
prototypeCell1
prototypeCell2
Then, go into the class thats responsible for the controller, and find the function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Then replace the code:
static NSString *CellIdentifier = #"PrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
With the code:
static NSString *CellIdentifier = #"ListPrototypeCell";
NSString* num = [NSString stringWithFormat:#"%d", indexPath.row];
NSString* actual = [CellIdentifier stringByAppendingString:num];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:actual forIndexPath:indexPath];
Everything else can stay the same. This code basically gets the row number, adds it to the identifier base, the gets the cell from the table using that string. So if its responding to a different row number, it return a different cell.
Just for clarification, my whole function for the problem is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListPrototypeCell";
NSString* num = [NSString stringWithFormat:#"%d", indexPath.row];
NSString* actual = [CellIdentifier stringByAppendingString:num];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:actual forIndexPath:indexPath];
cell.textLabel.text = self.content[indexPath.row];
return cell;
}
Good Luck!
Also, this is my first Answer/Question combo, so I really have no idea how to approach it, so if i've done anything wrong, please tell me and/or change it.
Thanks!
I'll put up my dumb solution for the sake of googlers...
This was my personal dumb mistake - when I created a new custom cell for my table view, I went to the top level directory of my project -> add new file -> and created an Empty File type under the OS X section rather than the iOS section.
Hopefully, most people have the other issue described above since it's less embarrassing :P
Sometimes you using storyboard and have collectionView inside it and collectionView as well. After that you decide to simplified your Storyboard and divide cell into another nib. You create empty nib, Ctrl+C from storyboard -> Ctrl+V into nib.
Everything looks fine but you'll have Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (PrettyCollectionViewCell) - nib must contain exactly one top level object which must be a UICollectionReusableView instance'
Ansver: do not do this. After I clean nib and add all of elements as I've in Storyboard - it fixed.
Seems like Bug of xCode IB - Version 7.3.1 (7D1014)
This error is mainly due to some extra views that are added by mistake. Have a look in the .xib file and check for extra unwanted view added by mistake. Delete that and it will work perfect. You can check for unwanted views by this is how it looks
In my case,
I have added UITableViewHeaderFooterView subclass and XIB in different target instead of actual target.
Make sure it is in running target.
I solved this by setting the class name in interface builder to match the cell reuse identifier (for that class) in interface builder.
Note: I'm not saying the class and identifier have to be the same. I'm saying they need to link the corresponding view and backing model.
I had this issue, finally found out that i had created UITableViewCell Subclass instead of CollectionViewCell subclass. it was evening, and i was tired,lol. Fixed it in the mng.
I dragged collection view cell from other project and got this.
'invalid nib registered for identifier (cell) - nib must contain exactly one top level object which must be a UICollectionReusableView instance' .
Then i compared the collection view cell from created my me and found reusable view missing. Hope this help.
It was a silly mistake on my side and I had two tableview cells which caused this error. Removed the other 'table view cell' which fixed the issue.

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