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"
Related
I am trying to find a way to initialise a view controller which has IBOutlets to a custom view that I have created in a XIB.
Here is the code where I initialise my class:
MyContentViewController *pageContentViewController = [[MyContentViewController alloc] init];
self.pageContent = pageContentViewController;
[pageContentViewController view];
MyContentViewController has the following properties:
#interface MyContentViewController : UIViewController
#property (nonatomic, weak) IBOutlet MyContentView *topLeftView;
#property (nonatomic, weak) IBOutlet MyContentView *topRightView;
#property (nonatomic, weak) IBOutlet MyContentView *bottomLeftView;
#property (nonatomic, weak) IBOutlet MyContentView *bottomRightView;
In MyContentViewController xib I have 4 views which I have set as MyContentView and have hooked the IBOutlet's above to the views in the storyboard.
The custom view is defined as following:
#import <UIKit/UIKit.h>
#interface MyContentView : UIView
#property (nonatomic, strong) IBOutlet UILabel *labelView;
#end
However when I run the application, I get the 4 views I have added in the MyContentViewController xib. However none of them display the UILabel from the custom UIView.
I have added the following in MyContentView:
- (void)awakeFromNib {
NSLog(#"VIEW AWAKE");
}
This get printed out so the views are being loaded. However how can I get them to display using the 4 IBOutlets.
Here is a picture of the app running with the view. There is no labels on the 4 views:
I see that your "MyContentView" has a nib of it's own. Loading a custom UIView class which has a nib layout from another nib is a bit more complex.
I think this blog post contains the answer
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'm using a combination of XCTest and OCMock for testing an iOS application. I'm using a UITableViewController to present a series of prototype cells which I would like to write some tests for. The cell itself is in a storyboard so I don't believe I can instantiate it from a nib.
Is the only option to use the viewController which uses the cell to instantiate it?
The custom cell class I'm using has a number of 'IBOutlets' connected to the prototype on the storyboard. The cell class looks something like this:
#interface QRFeedAdCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *mainImageView;
#property (strong, nonatomic) IBOutlet UIImageView *blurredImageView;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UIButton *someButton;
#property (strong, nonatomic) IBOutlet UILabel *someLabel;
#property (strong, nonatomic) IBOutlet UILabel *anotherLabel;
#property (nonatomic) BOOL isBusy;
#end
I've tried instantiating the cell using [[QRFeedAdCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"feedAdCell"]; but that will load the cell with all of the properties set to nil.
I've also tried registering the cell, but the tests also fail:
id mockTableView = [OCMockObject niceMockForClass:[UITableView class]];
[mockTableView registerClass:[QRFeedAdCell class] forCellReuseIdentifier:#"feedAdCell"];
QRFeedAdCell *cell = [mockTableView dequeueReusableCellWithIdentifier:#"feedAdCell"];
XCTAssertNotNil(cell, #"");
XCTAssertNotNil(cell.mainImageView, #"");
You can't.
The only way to test it is to instantiate the storyboard, then the view controller, then the cell itself, and then test the properties you set in the Interface Builder.
You can instantiate programmatically but first you need to register it by calling
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cellId"];
Then you can do custom styling to the cell or whatever you usually do in the storyboard like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellId" forIndexPath:indexPath];
// create and add a label
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 155, 21)];
label.text = #"text";
[cell addSubview:label];
...
}
This is working for me in a basic way. I havent gotten viewwillappear to run yet tho.
ViewdidLoad runs when you call .view on _vc.
#property (nonatomic, strong) Edit_ProfileVC *vc;
#property (nonatomic, strong) UIView *view;
#property (nonatomic) NSMutableDictionary *params;
UIStoryboard *mainSB = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
_vc = [mainSB instantiateViewControllerWithIdentifier:#"EditProfileVC"];
_vc.userData = [[EditUserTableDataSource alloc] initDataSourceWithJSON:self.params];
_view = _vc.view;
XCTAssertNotNil(_view, #"the view is not loading properly");
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