NIB文件中的静态表格单元格

是否可以创建一个具有自定义静态单元格的表视图的nib文件? 我想创建一个包含所有静态内容的表格式表格视图,但我目前没有使用故事板。 我能够在我的应用程序的默认Storyboard中找到内容类型菜单,但我正在使用Nib,当我创建UIViewController nib或UITableViewController nib时,在两种情况下,Attributes检查器中都没有内容类型菜单标签。

有什么想法吗?

现在看来,我正在努力做的事情就是不受支持。 我在Apple上提交了一个Radar bug,但这里的解决方法对我有用。

只需使用故事板,并使用以下方法将其称为笔尖:

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

在故事板中,您将要以EditProfile开头的视图控制器命名为本例。 希望这有助于某人。

这是一个过程,但它并不过分复杂:

  • 按下Command + N并在iOS> Source菜单下选择它,创建一个新的Cocoatouch类。

  • 为您的类命名,使其成为ViewController的子类,最后选中“还创建XIB文件”框。

  • 打开您的XIB文件并在身份检查器下定义自定义类以指向YourNewViewController的名称

工作的样本:在.h:

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

然后,在.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 

然后你需要做同样的事情,并使用一个“自定义单元格”(例如一个320×65视图的xib文件)和一个类来定义单元格。

 #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