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)
Related
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.
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.
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.
I have been through many questions on SO before asking this.
I have an issue where i have created a custom UITableView cell in a StoryBoard. I have subclassed UITableViewCell and exposed properties to link my various components to within the cell.
I am NOT creating or adding any components to the cell in
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I simply use the
[tableView dequeueReusableCellWithIdentifier:cleanseCell forIndexPath: indexPath];
call to reuse cells. I do some customisation on the cells but I don't add anything new. I just modify an image, some labels and a text field . The cell reuse identifier is set correctly both in the code and on the component in the storyboard.
The problem is that when the cell is reused, somewhere under the hood the UILabels are being duplicated. I have a UITextField in there as well - it doesn't have this problem. Only the UILabels are somehow duplicated.
So the cell presents fine the first time with the correct information. Then the next time the cell is created its still fine. The third time the cell shows up with Overlapping UILabels. One with the new text and one with the text of the original cell. At any rate there are two UILabels in the cell now where there was only one before and I didn't add it there.
Anyone else experienced this or have some comment to make?
EDIT:
This is my UITableViewCell - There is nothing in the implementation other than synthesising the properties (which is not even required anyway)
#import <UIKit/UIKit.h>
#interface SJCleanseNotificationCell : UITableViewCell
#property (nonatomic)float openHeight;
#property (nonatomic, strong)IBOutlet UIImageView *iconView;
#property (nonatomic, strong)IBOutlet UILabel *dateTimeLabel;
#property (nonatomic, strong)IBOutlet UILabel *titleLabel;
#property (nonatomic, strong)IBOutlet UITextView *messageLabel;
-(IBAction)dismiss:(id)sender;
-(IBAction)activate:(id)sender;
#end
And this is the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cleanseCell = #"CleanseCell";
NSDictionary *cleanse = sjTimer.cleanseNotification;
SJCleanseNotificationCell *cell;
if([_tableView respondsToSelector:#selector(dequeueReusableCellWithIdentifier:forIndexPath:)])
cell = (SJCleanseNotificationCell*)[_tableView dequeueReusableCellWithIdentifier:cleanseCell forIndexPath: indexPath];
else
cell = (SJCleanseNotificationCell*)[_tableView dequeueReusableCellWithIdentifier:cleanseCell];
[cell.dateTimeLabel setText:[cleanse objectForKey:#"date"]];
[cell.titleLabel setText:[cleanse objectForKey:#"title"]];
[cell.messageLabel setText:[cleanse objectForKey:#"message"]];
NSNumber *stage = (NSNumber*)[cleanse objectForKey:#"stage"];
if(stage)
[cell.iconView setImage:[[SJCleanseTimer instance]bottleImageForCleanseStage:stage.integerValue]];
cell.delegate = self;
cell.openHeight = 100;
return cell;
}
The problem was I had Clears Graphics Context unchecked in the inspector on the UILabel elements. After checking that checkbox it behaved normally.
I have a UICollectionView Cell that try to initialize a custom subview but giving me error.
//In Init - (id)initWithFrame:(CGRect)frame
DetailSubView *view=[[DetailSubView alloc]initWithFrame:CGRectZero];
self.contentView=view;---> ERROR
//.h file
#property (strong, nonatomic) DetailSubView *contentView;
Error
[DetailCustomCell setContentView:]: unrecognized selector sent to instance 0xe48a090
This is because the contentView property of a cell is read-only. To customize the cells content view, add subviews to it.