UITableView什么都没显示

时间:2013-08-17 00:37:49

标签: objective-c uitableview

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
  1. 开发环境是IOS SDK 6.1。 / simulator版本是6.0。
  2. 我在故事板中使用了表视图控制器。
  3. 我创建了一个名为“RetrieveDataFromDBTableVC”的类,并将表视图控制器的自定义类设置为“RetrieveDataFromDBTableVC”。 enter image description here

  4. 我认为代理和数据源已经为我设置了,所以我不需要再次设置了吗? (委托和数据源的连接是表视图)。

  5. enter image description here

    1. 在“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;
        }
      
    2. 我认为连接是问题,但我不知道如何解决这个问题。谢谢!!

2 个答案:

答案 0 :(得分:0)

确保从以下位置更改文件的这一行:(在.h文件中)

@interface YourClass : something

到:

@interface YourClass : something <UITableViewDataSource, UITableViewDelegate>

如果你还没有。

答案 1 :(得分:0)

除非它在IB中绘制为UITableViewController的实例,并且是UITableViewController的子类,否则委托和数据源自动连接。在IB中或在viewDidLoad上的代码中明确地连接它们。

相关问题