【问题标题】:Static Table Cells in NIB FileNIB 文件中的静态表格单元格
【发布时间】:2014-07-14 03:29:14
【问题描述】:

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

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    目前看来,我正在尝试做的事情不受支持。我在 Apple 上提交了 Radar 错误,但这是对我有用的解决方法。

    只需使用情节提要,并像笔尖一样使用:

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

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

    【讨论】:

    • 迄今为止最好的方法..!!在 '17 中使用相同的方法
    【解决方案2】:

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

    • 通过按 Command + N 并在 iOS > Source 菜单下选择它来创建一个新的 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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多