Error when set UICollectionViewController - nib but didn't get a UICollectionView - ios

I have a trouble when set custom cell into UICollectionViewController.
This is my code.
KidsDetailViewController.h
#import <UIKit/UIKit.h>
#interface KidsDetailViewController : UICollectionViewController <UICollectionViewDataSource>
#property NSNumber *idCampana;
#property (weak, nonatomic) IBOutlet UICollectionView *grid;
#end
KidsDetailViewController.m
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [grid_kid count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
GridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MY_CELL" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:self.truckImages[0]];
cell.prd_img.image = [UIImage imageNamed:#"ic_launcher_58x58"];
return cell;
}
GridCell.h
#import <UIKit/UIKit.h>
#interface GridCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *prd_img;
#end
GridCel.m
#import "GridCell.h"
#implementation GridCell
- (instancetype)initWithFrame:(CGRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
// Initialization code
}
return self;
}
#end
This is the Error.
*** Assertion failure in -[KidsDetailViewController loadView], /SourceCache/UIKit_Sim/UIKit-3283.1/UICollectionViewController.m:166
2014-07-28 02:25:18.874 Geelbe[7673:231598] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "jXd-Ir-mr4-view-0IK-5Q-NzC" nib but didn't get a UICollectionView.'
*** First throw call stack:
I've tried everything and still gives error.
Thanks.

I had this issue when adding a UICollectionViewController as an embedded container view controller on a storyboard.
I switched the container view controller's custom class to be a Collection View Controller. But the view property inside that view controller was still the Xcode boilerplate UIView.
Delete the default UIView within your controller on the storyboard and replace it with a UICollectionView. The outlets should be reconnected automatically.

I think the problem may be because you inherit from UICollectionViewController, you do not have to specify anymore , because it already implements it. Try top delete it, see if it would fix your error.
Also in the Identity Inspector make sure you set the class KidsDetailViewController for the corresponding View Controller.

Jeff Kelley's answer here worked for me for same error.
When you’re using a UICollectionViewController, the view outlet needs
to be connected to a UICollectionView in your storyboard; if it’s a
UIView of another class, it’ll crash.
So, I used UIViewController with UICollectionView as its property and hooked up with nib.

How to setup a collection view controller with a nib (.xib) file.
Add a new file by selecting the "Empty" template from the "User Interface" section. Drag a UICollectionView object from the object library over.
1) Associate the nib which collection view controller class.
In the Document Outline (left hand section), under "Placeholders", select "File's Owner". Show the Identity Inspector and set the class dropdown to your subclass of UICollectionViewController.
2) Associate the nib with the UICollectionView.
Again, in the Document Outline (left hand section), under "Placeholders", select "File's Owner". Right click it to see the "Outlets" and "Referencing Outlets" display. Two of the selections under "Outlets" are "view" and "collectionView". Its counter-intuitive, but drag from the "view" selection to the UICollectionView object in the Document Outline.
** If you drag from the "collectionView" selection, you will get the 'loaded the "your_collection_view_controller" nib but didn't get a UICollectionView' error.
When you build and run, the code should now "load" the nib and also "get" the UICollectionView.

Related

UICollectionReusableView Section Header crashes because of IBOutlet connection

I have a UICollectionViewController which delegates UICollectionViewDataSource and UICollectionViewDelegate. My collection view displays 2 sections with multiple rows of data and works fine.
I have created a Section Header (in IB Attributes Inspector -> Accessories) which then subclasses UICollectionReusableView with the SWOHighScoreHeader class:
#interface SWOHighScoreHeader : UICollectionReusableView
#property (strong, nonatomic) IBOutlet UILabel *hiScoreHead;
#end
I set this class (SWOHighScoreHeader) as the Custom Class for the UICollectionReusableView in IB.
In the UICollectionViewController I use the method:
-(UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
SWOHighScoreHeader *highScoreHeaderView = nil;
if ([kind isEqual:UICollectionElementKindSectionHeader]) {
highScoreHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:#"highScoreTableHead"
forIndexPath:indexPath];
}
return highScoreHeaderView;
}
The identifier highScoreTableHead is set as the UICollectionReusableView Collection Reusable View Identifier in IB.
At this stage, the section headers display correctly, albeit with the default label text.
My issue comes when I connect the IBOutlet UILabel hiScoreHead property with the outlet in IB. When I do this, the program crashes with:
Unknown class SWOHighScoreHeader in Interface Builder file.
** * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key submitButton.'
I've tried deleting the outlet connection and reconnecting but still nothing. Any ideas where I am going wrong?
Add the following property to SWOHighScoreHeader:
#property (weak) IBOutlet UIButton* submitButton;
Or try to find out which object in your storyboard is expecting the submitButton to actually exist.
An IBoutlet must always be a weak property, otherwise its container will not be deallocated.
I solved this by using Tags instead of IBOutlets ie
UILabel *hiScoreHeader = (UILabel *)[highScoreHeaderView viewWithTag:101];
hiScoreHeader.text = #"Header Text";
I'm not sure why the IBOutlets don't work but at least I have a solution.

is it possible to segue from a UITableViewCell on a UIView to another view

Xcode 4.6.1 iOS 6 using storyboards
My problem is this
I have a UITableView with dynamic prototype cells on a UIView in a UIViewController (that is itself embedded in a navigation controller) and I want to segue from one specific cell to another view
(Before anyone suggests I should just be using a UITableViewController , I do have other things on the UIView, so i'm set up this way for a reason.)
Now i'm not sure how to go about creating the segue
If I drag from the prototype UITableViewCell to create a segue , all the generated cells automatically call the the segue - when i need only one to do so. This is normal behaviour and I would get around this if i was using a UITableViewController by creating the segue by dragging from UITableViewController and calling [self performSegueWithIdentifier:.... From my didSelectRowAtIndexPathMethod so only the specific cell I want to perform this segue triggers it.
I don't have a UITableViewController in this case - just my UITableView on a UIView that is part of a UIViewController subclass
I've been playing around and I have just discovered that i cannot drag from the UITableView - doesn't let you do that, so that was a deadend.
My only choice that seemed left to me was to drag from the UIViewController
So i tried that and of course XCode throws up an error on the perform segue line telling me i have ... No visible interface for 'LocationTV' declares the selector performSegueWithIdentifier. LocationTv being my tableview subclass.
What is the correct way to attempt to call the new view in this situation
Thank
Simon
First of all segues can be use only between UIViewControllers. So in case you want to perform a segue between two views that are on the same view controller, that's impossible.
But if you want to perform a segue between two view controllers and the segue should be trigger by an action from one view (inside first view controller) well that's possible.
So in your case, if I understand the question, you want to perform a segue when the first cell of a UITableView that's inside of a custom UIView is tapped. The easiest approach would be to create a delegate on your custom UIView that will be implemented by your UIViewController that contains the custom UIView when the delegate method is called you should perform the segue, here is a short example:
YourCustomView.h
#protocol YourCustomViewDelegate <NSObject>
-(void)pleasePerformSegueRightNow;
#end
#interface YourCustomView : UIView {
UITableView *theTableView; //Maybe this is a IBOutlet
}
#property(weak, nonatomic) id<YourCustomViewDelegate>delegate;
YourCustomview.m
#implementation YourCustomview
# synthesise delegate;
//make sure that your table view delegate/data source are set properly
//other methods here maybe
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0) { //or any other row if you want
if([self.delegate respondsToSelector:#selector(pleasePerformSegueRightNow)]) {
[self.delegate pleasePerformSegueRightNow];
}
}
}
YourTableViewController.h
#interface YourTableViewController : UIViewController <YourCustomViewDelegate> {
//instance variables, outlets and other stuff here
}
YourTableViewController.m
#implementation YourTableViewController
-(void)viewDidLoad {
[super viewDidLoad];
YourCustomView *customView = alloc init....
customView.delegate = self;
}
-(void)pleasePerformSegue {
[self performSegueWithIdentifier:#"YourSegueIdentifier"];
}
You can create any methods to your delegate or you can customise the behaviour, this is just a simple example of how you can do it.
My Solution
I ended up using a delegation pattern
I made a segue dragging from the my UIViewController - specifically dragging from the viewController icon (the orange circle with a white square in it - from the name bar thats under the view in the storyboard - although you could also drag from the sidebar ) to the view that i wanted to segue to.
I needed to trigger this segue from a table view cell on a table view.
TableView Bit
So i declared a protocol in my tableview header file - which is called LocationTV.h - as follows
#protocol LocationTVSegueProtocol <NSObject>
-(void) makeItSegue:(id)sender;
#end
Below that I declare a property to hold my delegate
#property (nonatomic, strong) id<LocationTVSegueProtocol> makeSegueDelegate;
To actually trigger the segue i called the makeItSegueMethod on my makeSequeDelegate in my didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
DLog(#"selected row %d",indexPath.row);
case dLocation:
{
if(indexPath.row == 2){
[_makeSegueDelegate makeItSegue:self];
} else if (indexPath.row == 7){
UIViewController Bit
and set up my UIViewController (named MultiTableHoldingVC) as implementing that protocol
#interface MultiTableHoldingView : UIViewController
<EnviroTVProtocol,LocationTVSegueProtocol> {
}
Below that i declared the protocol method in the list of my classes methods (although i'm not sure that is necessary as the compiler should know about the method as the decalration of implementing a protocol is essentially a promise to implement this method)
-(void) makeItSegue:(id)sender;
And then over in the implementation file of my UIViewController i wrote the method which essentially just calls preformSegueWithIdentifier
-(void) makeItSegue:(id)sender{
[self performSegueWithIdentifier:#"ChooseCountryNow"
sender:sender];
}
And to link it all together,as in the header file I had declared my instance of the tableView as follows
#property (strong, nonatomic) IBOutlet LocationTV *dsLocationTV;
I had to set that tables views delegate property to be self - which I did in my UIViewControllers -(void)ViewDidLoad method
_dsLocationTV.makeSegueDelegate = self;
It all seems a bit of a kludge calling a method to call a method and allprog suggestion is simpler (I cant for the life of me work out why it threw up errors for me) but this works just fine . Thanks to both allprog and danypata for their suggestions.
Hope this is helpful to someone out there
performSegueWithIdentifier: is a method of the UIViewController class. You cannot call it on a UITableView instance. Make your view controller implement the UITableViewDelegate protocol and set it as the delegate for the UITableView.
Another option is that you don't use segues. In the same delegate method do:
OtherViewController ov = [[OtherViewController alloc] init<<some initializer>>];
// Or in case of storyboard:
OtherViewController ov = [self.storyboard instantiateViewControllerWithIdentifier:#"ovidentifier"];
// push view controller
[self.navigationController pushViewController:ov animated:YES];
If the delegate object is different from the view controller, then the easiest solution is to add a weak property to the delegate's class that keeps a reference to the viewController, like this:
#property (weak) UIViewController *viewController;
and set it up in the viewDidLoad of the viewController
- (void) viewDidLoad {
self.tableView1.viewController = self;
}
Make sure that the tableView1 property is declared like this:
#property (IBACTION) (weak) SpecialTableView *tableView1;
Sometimes using the storyboard is more painful than writing the code yourself.

Multiple UIViews in XIB of UITableViewCell

I've been trying to make a custom UITableViewCell with a UIScrollView.
I have a .xib which contains a UITableViewCell (that contains only a UIScrollView) and a UIView that is the content view for the cell's scrollView.
Alright, now here's the code for the UITableView that should display the custom cell :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.tableView registerNib:[UINib nibWithNibName:#"RecessCell" bundle:nil] forCellReuseIdentifier:#"Recess"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RecessCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Recess"];
return cell;
}
The header for the RecessCell class:
#import <UIKit/UIKit.h>
#interface RecessCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIView *contentView;
#end
scrollView is an outlet for the UIScrollView inside each cell.
contentView is an outlet for the UIScrollView's content view.
Well, so far everything seems legit, but when I run the app:
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class
is not key value coding-compliant for the key contentView.'
I've read plenty about that run-time error, all I could find is :
I need to verify that the contentView is actually declared in
the header. (as you can see, it is declared there)
I need to change UITableViewCell to RecessCell in cellForRow, which I did.
How can I fix this problem? is creating custom UITableViewCells so difficult?
You shouldn't create another outlet called contentView in your header file. The UITableViewCell already has a property called contentView.
Also if you are creating a .xib with a UITableVieCell and you are adding views to the UITableViewCell the views will be added to the content view.
So what you need is to go in your .xib file and remove the link between the view that is linked to your contentView IBOutlet. (You can also delete that view, but be careful so that you won't delete the subviews)

UITableView inside a UIView

I would put an UITableView inside a UIView. I created a XIB where I have a tableView.
#import <UIKit/UIKit.h>
#interface PointsViewController : UIViewController <UITableViewDelegate>
{
IBOutlet UITableView *tableView;
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
[tableView setDelegate:self];
}
I instantiate the PointViewController class from another class and add it to a UINavigationBar by means of a button:when I click the button, the PointsViewController'view (the tableView) shall open. But it does not.
What am I missing? I tried also to make PointsViewController as a subclass of UITableViewController which works, but no UITableView is displayed.
You will also need to make your ViewController a delegate for UITableViewDataSource.
#interface PointsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *tableView;
}
#end
...and support the corresponding methods.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
You need to also hook up the table's dataSource and delegate to the File's Owner. Otherwise the view controller doesn't know what table to send responses to.
In your XIB, select the table and open the Connection Inspector. Then drag the 'plus' sign next to dataSource to File's Owner to make the connection. Do the same for delegate and the table's referencing outlet.

Custom table cell: "class is not key value coding-compliant"

UPDATE: I decided to start over since I was still in the early stages of this app. I repeated everything and for whatever reason, the custom cell took the second time around. I will keep the old files around to confirm another answer, as I imagine I am not the only one who will have this problem.
I am building a tabbed application that requires custom cells in its table views. I have done this a few times and I always seem to hit a speed bump when hooking up these custom cells. The app booted up fine until I started using the custom cell in my table view controller titled SongsTVC. I am receiving a termination with a reason of:
[<SongsTVC 0x6831330> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key albumLabel.
I am using this tutorial and have used it before (changed a few things for ARC and iOS 5) with success. In fact, the code and IB layout I am using is based off of an already working project I have. I am aware of this error commonly presenting itself when you hook up your outlets to the file's owner and not the cell itself. I am not making this mistake but it is still giving me this error. So far, I have removed the label it has a problem with and even deleted the cell's files entirely in order to start over. Any help would be appreciated.
SongCell.h
#import <UIKit/UIKit.h>
#interface SongCell : UITableViewCell{
}
#property(nonatomic, assign) IBOutlet UILabel *titleLabel;
#property(nonatomic, assign) IBOutlet UILabel *artistLabel;
#property(nonatomic, assign) IBOutlet UILabel *albumLabel;
#end
SongCell.m
#import "SongCell.h"
#interface SongCell ()
#end
#implementation SongCell
#synthesize titleLabel, artistLabel, albumLabel;
#end
SongsTVC.h - Header of the TableViewController
#import <UIKit/UIKit.h>
#interface SongsTVC : UITableViewController{
UINib *cellLoader;
}
#end
SongsTVC.m - Relevant TableViewController methods
#import "SongsTVC.h"
#import "SongCell.h"
#interface SongsTVC ()
#end
static NSString *CellClassName = #"SongCell";
#implementation SongsTVC
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
cellLoader = [UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]];
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SongCell *cell = (SongCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName];
if (!cell)
{
//CRASH ERROR POINTS HERE
NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
// Configure the cell...
cell.titleLabel.text = #"SONG";
cell.artistLabel.text = #"Artist";
cell.albumLabel.text = #"Album";
return cell;
}
Interface Builder
NOTE: The cell identifier has been set to "SongCell" in IB and the file owner is UITableViewController because multiple tables will be using this cell. As long as the view is a table, it should work (it has in the past).
UPDATE: The xib file in XML format has been pasted here.
I had this exact problem today, while making a sample project for another SO answer! It looks like your xib is trying to connect an outlet to your view controller instead of your cell. In your screenshot, the outlets look correctly defined, but occasionally an old outlet definition can get left in the underlying XML and cause this type of crash.
If you've changed the files owner class after connecting some outlets, for example, this could confuse it.
You may be able to find it by opening the xib as "source code", look for the element and check there are only the entries you expect. Perhaps search the XML file for albumLabel as well.
If that doesn't work, you may have to scrap the xib and start again.
Unfortunately, the only solution that I have found was to start over. I did everything exactly as I did before and it worked the second time around. It was quite a chore having to scrap the entire thing and start over but it was the only way I could get it to work. I'm leaving this as the answer unless somebody can figure out what happened. (See original post and its update)
I had a similar issue and was able to solve it by rebuilding the Storyboard/NIB.

Resources