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

时间:2014-07-14 03:29:14

标签: ios objective-c ios7 xcode5 interface-builder

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

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

目前看来,我试图做的事情是不受支持的。我在Apple上提交了一个雷达漏洞,但这里的解决方法对我有用。

只需使用故事板,并使用:

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

在故事板中,在这种情况下,您将要作为EditProfile启动的视图控制器命名为。希望这可以帮助别人。

答案 1 :(得分:0)

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

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

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

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

这项工作的样本: 在.h:

@interface LocationsListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@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

然后你需要做同样的事情,并使用一个“自定义单元格”(例如一个320x65视图的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