Is it possible to create a nib file that has a table view with custom static cells? I want to create a form-like table view with all static content, but I'm not currently using storyboards. I was able to find the content type menu in the default Storyboard of my app, but I'm using Nibs, and when I create either a UIViewController nib or a UITableViewController nib, in both cases there is no content type menu in the Attributes inspector tab.
Any thoughts?
It seems like at the moment, what I'm trying to do is just not supported. I filed a Radar bug on Apple, but here's the workaround that worked for me.
Just use a storyboard, and call it like a nib using:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"EditProfile" bundle:nil];
EditProfileTVC *vc = [sb instantiateViewControllerWithIdentifier:@"EditProfile"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:vc animated:YES];
Where in your storyboard, you name the view controller you want to start on as EditProfile, in this case. Hope this helps someone out.
There's a process to this, but it's not overly complicated:
Create a new Cocoatouch class by pressing Command + N and selecting it under the iOS > Source menu.
Name your class, make it a subclass of ViewController and finally check the 'Also create XIB file' box.
Open your XIB file and define the Custom Class under the identity inspector to point to the name of YourNewViewController
Sample of this at work:
In .h :
@interface LocationsListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, weak) myMapManager* mapManager;
@property (nonatomic, weak) IBOutlet UITableView* tableView;
@property (nonatomic, weak) NSMutableArray* locations;
@end
Then, in .m:
#import "LocationsListViewController.h"
#import "CustomCellController.h"
#import "myMapAnnotation.h"
#import "DetailViewController.h"
//Define MKMap details, easier to change later
#define kCellHeight 70
#define kMainCellIdentifier @"mainCellIdentifier"
#define kMainCellNib @"CustomCell"
#define kDetailVCNib @"DetailViewController"
@implementation LocationsListViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//Define initial view titles and such
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"MCS Locator", @"MCS Locator");
self.tabBarItem.image = [UIImage imageNamed:@"list"];
self.tabBarItem.title = NSLocalizedString(@"Our Locations", @"Our Locations");
self.mapManager = [myMapManager sharedInstance];
self.locations = self.mapManager.locations;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:kMainCellNib bundle:nil] forCellReuseIdentifier:kMainCellIdentifier];
//Edit button creation, added to bar at top
UIBarButtonItem* edit = [[UIBarButtonItem alloc] initWithTitle:@"EDIT"
style:UIBarButtonSystemItemEdit
target:self
action:@selector(editList)];
self.navigationItem.rightBarButtonItem = edit;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.locations count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
myMapAnnotation* currentLocation = [self.locations objectAtIndex:indexPath.row];
CustomCellController *cell = [tableView dequeueReusableCellWithIdentifier:kMainCellIdentifier];
[cell configureCellWithLocation:currentLocation];
return cell;
}
//Custom methods
- (void)editList {
if (!self.tableView.editing) {
//Editing mode entered
[super setEditing:YES animated:YES];
[self.tableView setEditing:YES animated:YES];
[self.navigationItem.rightBarButtonItem setTitle:@"DONE"];
} else {
//Done editing
[super setEditing:NO animated:YES];
[self.tableView setEditing:NO animated:YES];
[self.navigationItem.rightBarButtonItem setTitle:@"EDIT"];
}
}
// Edit/delete method
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (UITableViewCellEditingStyleDelete == editingStyle) {
[self.locations removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
//Methods for the singleton and tableview data passing
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kCellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
myMapAnnotation* currentLocation = [self.locations objectAtIndex:indexPath.row];
DetailViewController* detailView = [[DetailViewController alloc] initWithNibName:kDetailVCNib bundle:nil];
detailView.location = currentLocation;
[self.navigationController pushViewController:detailView animated:YES]; //Push on top
}
@end
Then you need to do the same and make a "Custom Cell" to use (an xib file with a view of 320x65 for example) along with a class as above to define the cells.
#import "CustomCellController.h"
@implementation CustomCellController
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void)configureCellWithLocation:(myMapAnnotation*)location {
self.title.text = location.title;
self.subTitle.text = location.subtitle;
}
@end