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
Related
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:
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 am in a situation in which I need to create custom table cells. I have subclassed UITableViewCell:
#interface CustomTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *icon;
#property (weak, nonatomic) IBOutlet UILabel *name;
#property (weak, nonatomic) IBOutlet UILabel *timestamp;
#property (weak, nonatomic) IBOutlet UILabel *email;
#property (weak, nonatomic) IBOutlet UILabel *phone;
...
Icon, name and timestamp will always be required. Email and phone are optional labels. I initially looked into just hiding the 'optional' labels but that does not remove the space that they take up. So I am looking into building this tableviewcell programatically.
2 things:
I cannot find anywhere that builds this programatically with constraints so I am little stumped on how to even get started.
I have created some of it in IB, i.e. since image, name and timestamp will always be there I thought maybe this would be better to put in IB. Then just build out the other labels underneath name and timestamp:
I tried to create a new initMethod:
- (id)initWithUser:(User *) user {
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"userCell"];
if (self) {
// configure required stuff
[self.icon setImage:[PersonImage imageForTimestamp:user.timestamp]];
self.name.text = user.name;
self.timestamp.text = user.timestamp;
if (user.email != nil) {
//add email label with constraint tied to timestamp
}
if (user.phone != nil) {
//add phone label with constraint tied to timestamp or email
}
}
return self;
}
I set identifier to userCell in IB. However it does not seem to have my required labels. Can I do this? Is it a good idea? How do I create the labels with the correct position?
Part of your problem is that cells in an table view aren't initd every time. They get dequeued in order to save the overhead of creating a new cell every time. You will customize the data for the cell you're subclassing in -tableView:cellForRowAtIndexPath:.
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"
In my collectionView based app i want to cache the last 100 cells loaded, and i think best way for me is to work with core data.
I already have a custom class for UICollectionViewCell, and obviously all the cells are objects of this class. it's look kind of like this:
#interface Cell : UICollectionViewCell
#property (strong, nonatomic) NSString *cellFacebookId;
#property (strong, nonatomic) NSString *cellMail;
#property (strong, nonatomic) NSString *cellAddId;
#property (strong, nonatomic) NSString *cellCategory;
#property (strong, nonatomic) IBOutlet UIImageView *cellBackImg;
#property (strong, nonatomic) IBOutlet UIImageView *titleBarImage;
#end
Now I've created an entity named Cell, with same attributes. can i create a NSManagedObject subclass that will replace my original "Cell" class and still use it as UICollectionViewCell custom class?
You can't use a subclass of NSManagedObject as a UICollectionView cell. It has to be a subclass of UICollectionViewCell. Besides UICollectionView reuses cells by caching them.