开发者

Unable to view table in RootViewController

开发者 https://www.devze.com 2023-02-09 14:52 出处:网络
I have a navigation app that I am working on which for some reason is not allowing me to view my table on my initial screen (i.e. from the RootViewController).I have the following method that is calle

I have a navigation app that I am working on which for some reason is not allowing me to view my table on my initial screen (i.e. from the RootViewController). I have the following method that is called by my "viewDidLoad" method:`

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

` which does some work, and then calls the method:

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

This method does some work with an NSArray called "sortedArray" which is a property declared, and synthesized in 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:)];

This is how the above method ends. I then have the following code for my "cellForRowAtIndexPath" method:

- (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;

}

To me, everything looks fine. When I run my code, I have NSLog statements that issue output to the console which clearly shows me that the NSArray sortedArray contains data. Yet, when I run the code on my iPhone simulator in XCode, I get an empty table. Can anyone see what I'm doing wrong?

Thanks in advance to all who reply.


Have you connected your tableView to a property in interface builder?

Also, when your sorted array changes, you might want to call [myTabelView reloadData]; to refresh the table. Do this each time you change your sortedArray i.e.

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

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


Have you implemented tableView:numberOfRowsInSection: ?

This should probably return [sortedArray count].


Did you fill numbersOfRowInSection?

On a side note, I don't understand why you're copying everything then removing duplicates instead of skipping adds when your array contains the object.


Double check your tableview datasource property. If the datasource isn't set for your UITableView. Make sure that you are setting that to your object and make sure your object defines the -(NSInteger)tableView:numberOfRowsInSection: method.

That should return [sortedArray count].

0

精彩评论

暂无评论...
验证码 换一张
取 消