单元格不会出现在UITableViewController中

时间:2014-11-12 10:23:17

标签: ios objective-c xcode

我在UITableViewController中显示细胞时遇到问题,简单地说。我去测试时没有显示任何内容。以下是我的代码。

FormationViewController.m

#import <Foundation/Foundation.h>
#import "FormationViewController.h"

@interface FormationViewController ()

@end

@implementation FormationViewController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [self.names count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Title"
                                                            forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"Title"];
    }

    NSString *name = _names[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", name];
    return cell;
}

- (void)        tableView:(UITableView *)tableView
  didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        self.title = @"Formation View";
        self.tabBarItem.image = [UIImage imageNamed:@"tab_icon_formation"];
        self.names = @[@"John Doe", @"Lee Harvey Oswald", @"Pat Buchanan", @"Angry Orchard",
                       @"Sierra Nevad", @"Jeff Bridges", @"John McClain", @"Lucy Genero"];
    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"The length of names is %lu", self.names.count);
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    self.formationVC.frame = CGRectMake(
                                        0,
                                        self.topLayoutGuide.length,
                                        CGRectGetWidth(self.view.frame),
                                        CGRectGetHeight(self.view.frame)
                                        - self.topLayoutGuide.length
                                        - self.bottomLayoutGuide.length
                                        );
}

- (void)loadView {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];

    self.formationVC = [[UIView alloc] init];
    self.formationVC.backgroundColor = [UIColor whiteColor];
    [view addSubview:self.formationVC];

    self.view = view;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
@end

我的 FormationViewController.h

#import <UIKit/UIKit.h>

@interface FormationViewController : UITableViewController

@property (strong, nonatomic) UIView *formationVC;
@property (strong, nonatomic) NSArray *names;

@end

我也不确定我是否在正确的方法中添加了一些代码行。

我还认为我的问题可能在于tableView:cellForRowAtIndexPath:方法。我并不完全确定我正在100%正确实施。

如果有人能帮助我找出我做错了什么,请告诉我。这是我第一个Obj-C / iOS项目我也在努力,所以请温柔!谢谢!

4 个答案:

答案 0 :(得分:0)

好吧,我尝试了你的代码,没有使用xib并面临同样的问题。 然后我改变了两件事

删除了LoadView。 将self.names初始化从initWithNibName移动到viewDidLoad

添加了

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Title"];
[self.tableView reloadData];

to viewDidload 它工作了

答案 1 :(得分:0)

我认为你永远不会停留在(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil ...如果你从故事板中实例化你的视图控制器,你需要实现initWithCoder,或者只是在viewDidLoad中初始化你的名字}。

答案 2 :(得分:0)

由于您可能正在使用Storyboard,请检查以下步骤:

1)故事板 - 选择您的表格视图控制器,并在Identity Inspector中确保将它的类设置为您的FormationVC。

2)故事板 - 在控制器内选择表格视图并选择它的第一个单元格(建议您使用原型单元格)。确保它的样式设置为Basic并将其标识符设置为&#34; BasicCell&#34;。

3)转到您的代码并使用:

<强> FormationVC.h

#import <UIKit/UIKit.h>
@interface FormationVC : UITableViewController
@end

<强> FormationVC.m

#import "FormationVC.h"
@interface FormationVC ()
@property (strong, nonatomic) NSArray *names;
@end

@implementation FormationVC

#pragma mark - View Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    // you must not forget to call your custom init method once the view is loaded
    [self setupView];
}

- (void)setupView {

    // set title of VC
    self.title = @"Formation View";

    // create array of names
    _names = @[@"John Doe", @"Lee Harvey Oswald", @"Pat Buchanan", @"Angry Orchard",
               @"Sierra Nevad", @"Jeff Bridges", @"John McClain", @"Lucy Genero"];

    // log number of names in names array
    NSLog(@"The length of names is %lu", _names.count);

}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_names count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"%@", _names[indexPath.row]];

    return cell;
}

@end

答案 3 :(得分:-1)

问题在那里: -

@property (strong, nonatomic) NSArray *names;

NSArray更改为NSMutableArray

in.h

EX: - @property (nonatomic , retain) NSMutableArray *names

in.m

names = [[NSMutableArray alloc] init];

然后分配init你的数组然后把值放到它的工作。

相关问题