Objective c UITableView不显示任何内容

Objective c UITableView不显示任何内容,objective-c,uitableview,Objective C,Uitableview,当我运行应用程序以显示UITableView时,结果什么都没有。它将不会运行: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {} 及 开发环境为IOS SDK 6.1模拟器版本是6.0 我在故事板中使用了表视图控制器 我创建了一个名为“RetrieveDataFromDBTableVC”的类,并将表视图控制器的自定义类设置为“RetrieveDataFrom

当我运行应用程序以显示UITableView时,结果什么都没有。它将不会运行:

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section {} 

  • 开发环境为IOS SDK 6.1模拟器版本是6.0
  • 我在故事板中使用了表视图控制器
  • 我创建了一个名为“RetrieveDataFromDBTableVC”的类,并将表视图控制器的自定义类设置为“RetrieveDataFromDBTableVC”。

  • 我想委托和数据源已经为我设置好了,所以我不需要再次设置,对吗?(委托和数据源的连接是表视图)

  • 在“RetrieveDataFromDBTableVC”类中,我定义如下:

      - (void)viewDidLoad
      {
        [super viewDidLoad];
        [self retrieveData];
      }
    
      -(void)retrieveData
      {
          NSURL * url = [NSURL URLWithString:getDataURL];
          NSData * data = [NSData dataWithContentsOfURL:url];
    
          citiesArray = [[NSMutableArray alloc]init];
    
          // Assign data
          NSString * cID = @"aa";
          ...
    
          cities * myCity = [[cities alloc]initWithCityID:cID andCityID:cName andCityState:cState andCityPopulation:cPopulation andCityCountry:cCountry];
    
          // Add city object to our cities array
          [citiesArray addObject:myCity];
    
          // Create a myTableView outlet for this table view to reload data 
          // self.tableView is also correct right?
          [self.myTableView reloadData];
       }
    
       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section
       {
          // It will not run this function !!
          return [citiesArray count];
       }
    
       // It will not go into this function either !!
       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
         // Set the identifier name in UITableViewCell also
         static NSString *CellIdentifier = @"cities";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
         // Retrieve the current city object for use with this indexPath.row
        cities * currentCity = [citiesArray objectAtIndex:indexPath.row];
    
        cell.textLabel.text = currentCity.cityName;
    
        return cell;
      }
    

  • 我认为连接是个问题,但我不知道如何解决。谢谢

    确保将文件的这一行更改为:(在.h文件中)

    致:

    @interface YourClass:something
    

    如果你还没有

    除非它在IB中被绘制为UITableViewController的实例和UITableViewController的子类,否则委托和数据源不会自动连接。在IB或viewDidLoad上的代码中显式连接它们。

    您是否实现了
    -numberOfSectionsInTableView:
    且返回值为零?您不应该为表视图创建出口,默认情况下,表视图控制器有一个出口--您应该使用[self.tableView reloadData]@刘耀东:关于这个功能我什么都没碰过。默认返回值为0。当我开始运行应用程序时,它将进入此功能。@liuyaodong谢谢!!在我将返回设置为1之后。它显示了号码。再次感谢。这似乎是一个常见的误解。添加委托声明只是告诉编译器您打算实现这些协议,这不是强制性的。
      - (void)viewDidLoad
      {
        [super viewDidLoad];
        [self retrieveData];
      }
    
      -(void)retrieveData
      {
          NSURL * url = [NSURL URLWithString:getDataURL];
          NSData * data = [NSData dataWithContentsOfURL:url];
    
          citiesArray = [[NSMutableArray alloc]init];
    
          // Assign data
          NSString * cID = @"aa";
          ...
    
          cities * myCity = [[cities alloc]initWithCityID:cID andCityID:cName andCityState:cState andCityPopulation:cPopulation andCityCountry:cCountry];
    
          // Add city object to our cities array
          [citiesArray addObject:myCity];
    
          // Create a myTableView outlet for this table view to reload data 
          // self.tableView is also correct right?
          [self.myTableView reloadData];
       }
    
       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section
       {
          // It will not run this function !!
          return [citiesArray count];
       }
    
       // It will not go into this function either !!
       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
         // Set the identifier name in UITableViewCell also
         static NSString *CellIdentifier = @"cities";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
         // Retrieve the current city object for use with this indexPath.row
        cities * currentCity = [citiesArray objectAtIndex:indexPath.row];
    
        cell.textLabel.text = currentCity.cityName;
    
        return cell;
      }
    
    @interface YourClass : something
    
    @interface YourClass : something <UITableViewDataSource, UITableViewDelegate>