I have identical code in multiple view controllers and can't figure out why the Xcode compiler won't recognize a custom Cell.
In storyboards, the VCs have a view controller containing a tableView with 1 dynamic cell.
This cell is set in identity inspector to a custom cell.
In the file for the VC, in CellForRowAtIndexPath I have the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
myCustomCell* cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
The only difference between the custom cells is some were created in older versions of Xcode and don't have the NS_ASSUME_NONNULL_BEGIN and END lines:
#import <UIKit/UIKit.h>
#interface contactCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet UILabel *someLabel;
#end
vs.
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface ideaCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet UILabel *someLabel;
#end
NS_ASSUME_NONNULL_END
However, if I set a breakpoint after the working one it shows:
However, if I set a breakpoint after the non-working on it shows a generic uitableviewcell.
Why isn't it recognizing the custom Cell?
I have tried deleting the cell and recreating it as well as cleaning the build folder.
Thanks for any suggestion.
Edit:
Here is storyboard:
Related
I need some help with a uicollectionview that I've been trying to implement in Objective-C/xcode: I am unable to have the cells appear/populate the collectionview (CV).
Currently I am using the interface builder, and a custom class for cells.
The CV is to be a wide strip that is a single row of square image cells, that can be scrolled/swept through horizontally.
My attempts at implementation result in the wide strip but it never populates with cells.
I have explored other stackoverflow threads similar to this issue, where they discuss the subclassing of cell but I was unable to make it work. Have also tried programmatic implementation and had the same issue. Would really appreciate any insight as I have been stuck at this point for days. Below the code I am using, are screenshots of interface builder showing different settings... some I know are useful to include and I apologize if I have shared anything useless.
from ViewController.h
#interface ViewController : UIViewController <UICollectionViewDelegate,UICollectionViewDataSource>
#property (weak, nonatomic) NSMutableArray *medCollectionCells;
from ViewController.m
#import "CoolCell.h"
#property (weak, nonatomic) IBOutlet UICollectionView *medCollection;
- (void)viewDidLoad
{
_medCollectionCells = [NSMutableArray arrayWithObjects:#"1st cell",#"2nd cell",#"3rd cell",#"4th cell",#"5th cell",#"6th cell",#"7th cell",#"8th cell",#"5th cell", nil];
self.medCollection.delegate = self;
self.medCollection.dataSource = self;
}
// collection view delegate methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [_medCollectionCells count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CoolCell *cell = (CoolCell *)[_medCollection dequeueReusableCellWithReuseIdentifier:#"MyCustomCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
return cell;
}
CoolCell.h:
#import <UIKit/UIKit.h>
#interface CoolCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UILabel *customLabel;
#property (weak, nonatomic) IBOutlet UIImageView *customImage;
#end
CoolCell.m:
#import "CoolCell.h"
#implementation CoolCell
#end
Screen Shots
Storyboard layout
Collection View Settings 1
Collection View Settings 2
Cell Settings 1
Cell Settings 2
Interface Builder Outlets
Grateful for any assistance/insight. Thank you.
Seems like the delegate and dataSource properties are connected to a View object in Interface Builder. Maybe remove that since you are setting those properties in code.
I am trying to remove fields from a custom UITableViewCell (MyTableViewCell), but when I run dequeueReusableCellWithIdentifier and look at the debug console, it shows me the fields that I have deleted!
Here is the debug console. You can see its of type MyTableViewCell
But here is the MyTableViewCell class. It doesn't have fields titleLabel, gradientTitleBackground, or myImageView. (because I have deleted them)
#import <UIKit/UIKit.h>
#import "RouteImageView.h"
#interface MyTableViewCell : UITableViewCell
#property (nonatomic, weak) IBOutlet RouteImageView *routeImageView;
#property (nonatomic, weak) IBOutlet UILabel *totalMilesLabel;
#property (nonatomic, weak) IBOutlet UILabel *progressLabel;
#property (nonatomic, weak) IBOutlet UIProgressView *progressView;
#property (nonatomic, weak) IBOutlet UIImageView *medalPlaceholder;
#property (nonatomic, weak) IBOutlet UIImageView *medal;
#end
The next two shots show my storyboard configuration. You can see that I have a tableview cell with reuse ID: "walk_item", and that this cell is mapped to the class MyTableViewCell...
And the line where I load the cell (and where I read the debug console) is here:
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"walk_item"];
So why isn't the cell being properly updated? This is a problem because when I try to add new fields and connect them to IABOutlet's, the program crashes with EXEC_BAD_ACCESS! Something is not connecting properly between storyboard and my custom UITableViewCell class. Any suggestions on ideas how to debug this are greatly appreciated.
Use dequeueReusableCellWithIdentifier:forIndexPath instead of simple dequeueReusableCellWithIdentifier. I also have strange issues with table, when using that function
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"walk_item" forIndexPath:indexPath];
The answer was to do a clean build.
I'm trying to create a form like view using custom cells in uitableview. I have 3 custom cells, one with 6 UITextFields, one with just one, and one with a UITextView. It displays perfectly, and scrolls up and down with no problems. My problem is that whenever I select anything, either a textField or TextView it crashes with EXC_BAD_ACCESS. My break point shows that the textfields are not nil.
Any ideas about what may be causing my crash?
custom cell - properties in .h file - delegates set in custom cell class
#interface CandidateInfo1Cell : UITableViewCell
#property (nonatomic, strong) IBOutlet UITextField *firstNameText;
#property (nonatomic, strong) IBOutlet UITextField *lastNameText;
#property (nonatomic, strong) IBOutlet UITextField *cityText;
#property (nonatomic, strong) IBOutlet UITextField *stateText;
#property (nonatomic, strong) IBOutlet UITextField *emailText;
#property (nonatomic, strong) IBOutlet UITextField *phoneText;
#end
registering my nib:
UINib *infoCellNib = [UINib nibWithNibName:REUSE_INFO_CELLID bundle:[NSBundle bundleForClass:[InfoCell class]]];
[self.listTable registerNib:infoCellNib forCellReuseIdentifier:REUSE_INFO_CELLID];
cellForRowAtIndexPath:
InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSE_INFO_CELLID];
[self configureInfoCell:(InfoCell *)cell forRowAtIndexPath:indexPath];
return cell;
I have source code that does not use storyboard or xib. It contains a UITableView and the cells use the standard cell.textlabel.
What i want is to use a custom TableViewCell, normally i would create a UITableViewCell class and connect them in storyboard, but i cant connect them here since its not using storyboard or xib.
I have following UITableViewClass
#interface chatCell : UITableViewCell
#property(nonatomic, weak) IBOutlet UIImageView *homeTeamImage;
#property(nonatomic, weak) IBOutlet UILabel *homeTeamLabel;
#property(nonatomic, weak) IBOutlet UIImageView *awayTeamImage;
#property(nonatomic, weak) IBOutlet UILabel *awayTeamLabel;
#end
How can i place them inside the TableViewCell and connect them to the properties?
so i can start using
cell.homeTeamImage
cell.homeTeamLabel
cell.awayTeamImage
cell.awayTeamLabel
You don't need to use IBOutlet, since you don't want to use the properties in IB
#interface chatCell : UITableViewCell
{
UIImageView *homeTeamImage;
UILabel *homeTeamLabel;
UIImageView *awayTeamImage;
UILabel *awayTeamLabel;
}
#property(nonatomic, weak) UIImageView *homeTeamImage;
#property(nonatomic, weak) UILabel *homeTeamLabel;
#property(nonatomic, weak) UIImageView *awayTeamImage;
#property(nonatomic, weak) UILabel *awayTeamLabel;
#end
and don't forget to
#synthesize homeTeamImage, ...;
in your implementation file.
You can override the - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath. Instead of returning a standard UITableViewCell you can return yours
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell c = [[chatCell alloc] init];
return c;
}
You don't need the IBOutlet keyword: it isn't a type, it's only used by xcode when you use the storyboard. I think that without storyboard you should change the properties to strong pointers because no one else is strongly pointing to them so they'll be deleted.
The #synthesize is not mandatory if you use xcode 5. It's required only if you override both setter and getter of a property.
See Programmatically Adding Subviews to a Cell’s Content View from Table View Programming Guide for iOS. In this example cells are created in tableView:cellForRowAtIndexPath: but you can use same approach (add views on cell's content view) in UITableViewCell subclass.
#interface chatCell : UITableViewCell
{
UIImageView *homeTeamImage;
UILabel *homeTeamLabel;
UIImageView *awayTeamImage;
UILabel *awayTeamLabel;
}
#property(nonatomic, weak) UIImageView *homeTeamImage;
#property(nonatomic, weak) UILabel *homeTeamLabel;
#property(nonatomic, weak) UIImageView *awayTeamImage;
#property(nonatomic, weak) UILabel *awayTeamLabel;
#end
and don't forget to
enter code here
#synthesize "All properties"
I have created a custom UITableViewCell that contains 3 UILabels and a UIImageView. I am setting this up via storyboards and have a UITableView class that is connected to the table cell. In that class I am wanting to handle the different UI objects and have the option to pass in information from my view controller.
Here is the .h of my table cell view class
#property (retain, nonatomic) IBOutlet GBPathImageView *ProfileThumbnailImageView;
#property (weak, nonatomic) IBOutlet UILabel *profileDisplayNameLabel;
#property (weak, nonatomic) IBOutlet UILabel *profileUniversityNameLabel;
#property (weak, nonatomic) IBOutlet UILabel *profileNumberOfStringsLabel;
#property (strong, nonatomic) NSString *profileDisplayName;
#property (strong, nonatomic) UIImage *profileThumbnailImage;
#property (strong, nonatomic) NSString *profileUniversityName;
#property (nonatomic) NSUInteger profileNumberOfStrings;
- (id)initWithProfileImage:(UIImage *)profileImage
displayName:(NSString *)displayName
universityName:(NSString *)universityName
numberOfUserStrings:(NSUInteger)numberOfStrings;
As you can see I have a property outlet that all of the storyboard objects are connected to. I also have properties for each piece of necessary information. In my view controllers
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method I am wanting to instantiate this custom cell with information that I pass in. I am wanting to set the information properties with the correct info and have it be setup in the view class.
Here is the code inside the previously mentioned method:
static NSString *cellIdentifier = #"UserProfileCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if ([cell isKindOfClass:[StringrUserTableViewCell class]]) {
StringrUserTableViewCell * userProfileCell = (StringrUserTableViewCell *)cell;
userProfileCell.profileDisplayName = #"Alonso Holmes";
//userProfileCell.profileDisplayNameLabel.text = #"User Name";
}
I have two lines there, one is setting the information property for the name, and I have it in the view's .m file to set its text to that properties name. The problem that is happening is that the profileDisplayName property is null at the time it tries to set that UILabels text. How can I set this information at a time so that it will be there before the cell is loaded or when it's initially loading? You can also see a commented out line where I directly set the text of the UILabel right there. I would prefer not to do it this way because I want to leave the abstraction/logic to the view's class. The image view I setup is custom so it takes more code.
You are trying to set your text in profileDisplayName which is NSString type not a UILabel class. ( see #property (strong, nonatomic) NSString *profileDisplayName; ).
Change your line of code:
here is the problem
userProfileCell.profileDisplayName = #"Alonso Holmes"; //Use Label Object instead of NSString