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.
Related
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.
I have two separate xib. One hold main screen as child of UIView and also UICollectionView. Second xib contains UICollectionCell. If I just run my code as it is, it is working. (First xib with UIView is used in storyboard, its places in UIViewController).
I have this in - (void) awakeFromNib
UINib *cellNib = [UINib nibWithNibName:#"ViewCellNIB" bundle:nil];
[self.todayScroll registerNib:cellNib forCellWithReuseIdentifier:#"TodayCellID"];
Now, I want to add some IBOutlet connections into ViewCellNIB. If I do this, my code crash in runtime with this error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0xb068b10> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key temporaryLabel.'
I have this:
#property (weak, nonatomic) IBOutlet UILabel *temporaryLabel;
connected to UILabel on UICollectionViewCell
How can I repair this ?
Ok.. I have found solution. My mistake. I have incorrectly connect IBOutlet in Designer. I have to connect outlets via main UICollectionViewCell and not directly by dragging connection from element to code.
Question is that the contents of cell in tableview are not show!!
List part of codes
1. In the view controller
#property (strong, nonatomic) IBOutlet UITableView *mtableview;
2. In viewDidLoad
self.mtableview.delegate = self;
self.mtableview.dataSource = self;
[self.mtableview registerClass:[MyTableViewCell
class]forCellReuseIdentifier:#"MytableViewcell" ];
3. In tableview cellForRowAtIndexPath:
MyTableViewCell *mcell = [tableView
dequeueReusableCellWithIdentifier:#"MytableViewcell" forIndexPath:indexPath];
mcell.mdataname.text = [mdataArray objectAtIndex:indexPath.row];
4. In MyTableViewCell
#property (strong, nonatomic) IBOutlet UILabel *mdataname;
5. I found the mcell not nil , it has been alloc , but mcell.mdataname is nil
6. If I use storyboard to set the MyTableViewCell identifier , and remove the
[self.mtableview registerClass:[MyTableViewCell
class]forCellReuseIdentifier:#"MytableViewcell" ];
the contents of cell in tableview are show !!
7. So we can't set the identifier of cell programmatically ?
It looks like you should have an XIB associated with the custom cell class, and that you should be loading the associated NIB file in the code and registering the NIB (instead of registering the class).
Either that, or your class isn't properly configuring itself in the initWithStyle:reuseIdentifier: method (though this option seems less likely as you are using IBOutlet).
At the moment, whichever issue you have, mdataname is not created / loaded from the archive.
This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 7 years ago.
I'm getting a crash when running on an iOS6 device but NOT an iOS 7 device. I have a custom UITableViewCell with a xib LanguageCell.xib. It has 2 labels, a button, and a view. I have a class LanguageCell where I have 4 IBOutlets:
#interface LanguageCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *titleLbl;
#property (strong, nonatomic) IBOutlet UIButton *buyButton;
#property (strong, nonatomic) IBOutlet UILabel *saleLbl;
#property (strong, nonatomic) IBOutlet UIView *separatorLine;
I have connected all 4 views to their properties so the connections pane looks like this:
When I run the app, I get a crash when loading this table:
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<LanguageCell 0x1f5160c0> setValue:forUndefinedKey:]: this class
is not key value coding-compliant for the key buyButton.'
Why am I getting this problem on iOS6 but not iOS7?
EDIT:
I should add that the Custom Class is set properly on the xib
EDIT 2:
When I clear all the IB connections and run the code, the line if ([currentObject isKindOfClass:[LanguageCell class]]) returns false when it should be true, thus cell remains nil
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
LanguageCell *cell = (LanguageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"LanguageCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[LanguageCell class]])
{
cell = (LanguageCell *)currentObject;
break;
}
}
}
I use the same exact code for 3 other tables in my app and it works just fine. I don't understand why this one is giving me problems.
I had exactly the same problem. This normally just happens if you connected an element inside the Interface Builder with a method which doesn't exist anymore.
I also used the same class name. After changing the name, everything worked for me even on iOS 6.0.
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)