运行iOS模拟器时,表视图单元格不显示

时间:2014-03-18 08:24:28

标签: ios iphone objective-c cocoa-touch

我正在尝试创建一个表,但是当我运行模拟器时,我输入的数据都没有显示。我正在尝试创建一个简单的地址簿应用程序,到目前为止,这就是我所做的:

  1. 在空的故事板上,我从对象库中添加了一个View Controller,然后在视图控制器中添加了一个Table View。然后我将视图控制器嵌入到导航控制器中。我还在最后添加了另一个View Controller作为“详细信息”视图。 enter image description here

  2. 在我的viewController.h中,我添加了以下代码:

    @property (strong, nonatomic) NSArray *contact;
    @property (strong, nonatomic) NSArray *contactDetails;
    @property (nonatomic) NSInteger selectedIndex;
    
  3. 联系人将充当显示电话簿中所有联系人的阵列(这是我在显示时遇到的问题)

    用户选择联系人后,

    contactDetails会显示。

    selectedIndex是告诉应用程序选择了哪个联系人的方式。

    1. 在我的viewController.m中,我添加了以下代码:

      #import "HWViewController.h"
      
      @interface HWViewController ()
      
      @end
      
      @implementation HWViewController
      
      - (id)initWithStyle:(UITableViewStyle)style
      {
           self = [super initWithStyle:style];
                if (self) {
                    // Custom initialization
                }
           return self;
      }
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
      
          [self.tableView registerClass: [UITableViewCell class] forCellReuseIdentifier:@"Cell"];
      
          self.contact = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects:@"Joe", @"Simon", nil]];
      
          self.contactDetails = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects: @"+689171898057", @"+689173104153", nil]];
      
      }
      
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
      
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
          // Return the number of sections.
          return 1;
      } 
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          // Return the number of rows in the section.
          return self.contact.count;
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      
          static NSString *CellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
      
          // Configure the cell...
      
          if(cell==nil){
      
              cell = [[[NSBundle mainBundle] loadNibNamed:@"SampleCell" owner:nil options:nil] objectAtIndex:0];
      
          }
      
          UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
          label.text = [self.contact objectAtIndex:indexPath.row];
          [cell.contentView addSubview:label];
      
          return cell;
      } 
      
      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
          //UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
          self.selectedIndex = indexPath.row;
          [self performSegueWithIdentifier:@"TranslationSegue" sender:self];
      }
      
      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
      {
          // Get the new view controller using [segue destinationViewController].
          // Pass the selected object to the new view controller.
          UIViewController *vc = [segue destinationViewController];
          UILabel *details = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 250, 32)];
          details.text = [self.contactDetails objectAtIndex:((HWViewController*)sender).selectedIndex];
          [vc.view addSubview:details];
      }
      
      
      @end
      
    2. 之后,我进行了模拟,桌面上没有显示任何内容。

3 个答案:

答案 0 :(得分:0)

桌面视图中没有单元格。所以在你的故事板中添加tableview单元

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

enter image description here

答案 1 :(得分:0)

在viewController.h中添加:

@property(nonatomic, strong) IBOUTLET UITableView * TableView;

然后将此插座链接到您的tableview,然后尝试一下。

表视图中没有单元格,因为默认情况下原型单元格将设置为。现在选择tableview转到属性检查器并将原型单元格设置为1,然后尝试使用正确的单元格标识符运行程序。它将起作用。

HTH:)

答案 2 :(得分:0)

在viewdidload方法中设置tableView委托和DataSource协议。

- (void)viewDidLoad
     {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

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

 self.tableview.delegate = self;
 self.tableview.dataSource = self;

self.contact = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects:@"Joe", @"Simon", nil]];

self.contactDetails = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects: @"+689171898057", @"+689173104153", nil]];

}