2011-02-07 30 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]。