2011-02-07 53 views
1

我有一个导航应用程序,由于某些原因,我不能在初始屏幕(即RootViewController)上查看我的表格。我有以下方法由我“的viewDidLoad”方法叫:'无法在RootViewController中查看表格

- (void) locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation { 

` ,做一些工作,然后调用该方法:

- (void) readRestaurantsFromDatabase:(double)userLatitude 
       withUserLocation:(double)userLongitude{ 

这个方法做了一些工作,一个NSArray的所谓的“sortedArray”,这是一个属性声明,并且在合成RootViewController的:

//compile a list of categories 
     NSMutableArray *categoryList = [[NSMutableArray alloc] init]; 
     [categoryList addObject:@"All Types"]; 

     for (Restaurant *restCat in restaurants){ 

      [categoryList addObject:restCat.category]; 
     } 


     //remove duplicates 
     NSArray *copy = [categoryList copy]; 
     NSInteger index = [copy count] - 1; 

     for (NSString *restCategory in [copy reverseObjectEnumerator]) { 

      if ([categoryList indexOfObject:restCategory inRange:NSMakeRange(0, index)] != NSNotFound) { 
       [categoryList removeObjectAtIndex:index]; 
      } 

      index--; 

     } 

     [copy release]; 

     //put list in alphabetical order 
     sortedArray = [categoryList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

这是上述方法如何结束。然后,我有以下的代码为我 “的cellForRowAtIndexPath” 的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell. 

NSString *cellValue = [sortedArray objectAtIndex:indexPath.row]; 
cell.textLabel.text = cellValue; 

return cell; 

}

对我来说,一切都看起来不错。当我运行我的代码时,我有NSLog语句输出到控制台,清楚地显示出NSArray sortedArray包含数据。然而,当我在XCode的iPhone模拟器上运行代码时,我得到一个空表。任何人都可以看到我做错了什么?

在此先感谢所有回复的人。

回答

0

您是否已将您的tableView连接到界面构建器中的属性?

另外,当您的排序数组更改时,您可能需要拨打[myTabelView reloadData];来刷新表格。这难道每次你改变你的sortedArray即

//put list in alphabetical order 
sortedArray = [categoryList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

// The sorted array has changed, get the table view to refresh 
[myTabelView reloadData]; 
+0

非常感谢您的及时回复。我在哪里放置这段代码(即哪种方法)?会是:cellForRowAtIndexPath或将它是:readRestaurantsFromDatabase?再次感谢你的帮助。保重。 – syedfa 2011-02-07 18:42:57

0

你实现了tableView:numberOfRowsInSection:?

这应该可能返回[sortedArray count]。

0

填好了numbersOfRowInSection

在附注中,我不明白为什么要复制所有内容,然后删除重复项而不是在数组包含对象时跳过添加项。

0

仔细检查您的tableview DataSource属性。如果数据源没有为你的UITableView设置。确保你将它设置为你的对象,并确保你的对象定义了 - (NSInteger)tableView:numberOfRowsInSection:方法。

这应该返回[sortedArray count]。