简体   繁体   English

单元格不出现在UITableViewController中

[英]Cells Do Not Appear in UITableViewController

I'm having an issue with displaying cells in my UITableViewController , simply..nothing shows up when I go to test. 我在UITableViewController显示单元格时遇到问题,简单地..当我去测试时什么都没有显示。 Below is my code. 下面是我的代码。

FormationViewController.m 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

And my FormationViewController.h 还有我的FormationViewController.h

#import <UIKit/UIKit.h>

@interface FormationViewController : UITableViewController

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

@end

I'm also not sure if I'm putting some of these lines of code in the right methods. 我也不确定是否要在正确的方法中放入这些代码行。

I also think that my problem might be lying in the tableView:cellForRowAtIndexPath: method. 我还认为我的问题可能出在tableView:cellForRowAtIndexPath:方法中。 I'm not absolutely sure I'm implementing that 100% correctly. 我不确定绝对能正确实现100%。

If anyone can help me find out what I'm doing wrong please let me know. 如果有人可以帮助我找出我在做什么,请告诉我。 This is my first Obj-C/iOS project I'm working on too so please be gentle! 这也是我正在进行的第一个Obj-C / iOS项目,请保持警惕! Thank you! 谢谢!

Well, I tried your code , using no xibs and faced the same problem. 好吧,我不使用xibs尝试了您的代码,并且遇到了同样的问题。 Then I changed 2 things 然后我改变了两件事

removed LoadView. 删除了LoadView。 moved self.names intialization from initWithNibName to viewDidLoad 将self.names初始化从initWithNibName移到viewDidLoad

added 添加

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

to viewDidload And it worked 查看Didload,它起作用了

我认为您永远不会停止在(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil ...如果从情节(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil实例化视图控制器,则需要实现initWithCoder ,或者只将名称初始化在viewDidLoad

As you are probably using Storyboard, check these steps: 由于您可能正在使用情节提要,请检查以下步骤:

1) Storyboard - select your table view controller and in Identity Inspector make sure you have it's class set to your FormationVC. 1)情节提要-选择表视图控制器,然后在Identity Inspector中确保已将其类设置为FormationVC。

2) Storyboard - select table view inside the controller and select it's first cell (suggesting you use Prototype Cells). 2)情节提要-选择控制器内的表视图,然后选择它的第一个单元格(建议您使用原型单元格)。 Make sure it's style is set to Basic and set it's identifier for example to "BasicCell". 确保将其样式设置为“基本”,并将其标识符设置为“ BasicCell”。

3) Go to your code and use this: 3)转到您的代码,并使用此代码:

FormationVC.h 编队VC

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

FormationVC.m 编队VC

#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

Problem IS there :- 问题在那里:-

@property (strong, nonatomic) NSArray *names;

change NSArray To NSMutableArray NSArray更改为NSMutableArray

in.h

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

and in.m in.m

names = [[NSMutableArray alloc] init];

and then alloc init your Array and then put value its work . 然后分配初始化你的数组,然后重视它的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM