Cellules de table statiques dans un file NIB

Est-il possible de créer un file nib qui a une vue de table avec des cellules statiques personnalisées? Je souhaite créer une vue de tableau de type formulaire avec tout le contenu statique, mais je n'utilise pas de storyboard pour le moment. J'ai été capable de find le menu type de contenu dans le Storyboard par défaut de mon application, mais j'utilise des Nibs, et quand je crée une nib UIViewController ou une nib UITableViewController, dans les deux cas il n'y a pas de menu type de contenu dans l'inspecteur languette.

Des pensées?

Il semble que pour le moment, ce que j'essaie de faire ne soit simplement pas supporté. J'ai déposé un bug Radar sur Apple, mais voici la solution de contournement qui a fonctionné pour moi.

Utilisez simplement un storyboard et appelez-le comme une plume en utilisant:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"EditProfile" bundle:nil]; EditProfileTVC *vc = [sb instantiateViewControllerWithIdentifier:@"EditProfile"]; vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self.navigationController pushViewController:vc animated:YES]; 

Où, dans votre storyboard, vous nommez le controller de vue sur lequel vous voulez démarrer en tant que EditProfile, dans ce cas. J'espère que cela aide quelqu'un.

Il y a un process à cela, mais ce n'est pas trop compliqué:

  • Créez une nouvelle class Cocoatouch en appuyant sur Commande + N et en la sélectionnant dans le menu iOS> Source.

  • Nommez votre class, en faire une sous-class de ViewController et enfin cochez la case 'Créer aussi le file XIB'.

  • Ouvrez votre file XIB et définissez la class personnalisée sous l'inspecteur d'identité pour pointer sur le nom de YourNewViewController

Exemple de ceci au travail: In .h:

 @interface LocationsListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, weak) myMapManager* mapManager; @property (nonatomic, weak) IBOutlet UITableView* tableView; @property (nonatomic, weak) NSMutableArray* locations; @end 

Puis, en .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:(NSSsortingng *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { //Define initial view titles and such self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = NSLocalizedSsortingng(@"MCS Locator", @"MCS Locator"); self.tabBarItem.image = [UIImage imageNamed:@"list"]; self.tabBarItem.title = NSLocalizedSsortingng(@"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 

Ensuite, vous devez faire la même chose et faire une "cellule personnalisée" à utiliser (un file xib avec une vue de 320×65 par exemple) avec une class comme ci-dessus pour définir les cellules.

 #import "CustomCellController.h" @implementation CustomCellController - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSSsortingng *)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