0

有人可以帮我按距离对数组进行排序并在 TableView 中显示吗?我创建了一个属性“sortedArray”并进行了综合。我创建了一个“sortList”方法并在 didUpdateToLocation 下调用它。但是,它似乎没有对数组进行排序,或者我没有将更新的结果放入我的 TableView 中。

@property(nonatomic, strong) NSArray *sortedArray;
-(NSArray *) sortList;
-(NSMutableArray *) barList;

@synthesize sortedArray;

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

    if (newLocation.horizontalAccuracy <= 100.0f) {
        [locMgr stopUpdatingLocation];
    }
    NSLog(@"Lat: %f, Long: %f", currentLoc.coordinate.latitude, currentLoc.coordinate.longitude);

    [self barList];
    [self sortList];
    [self.tableView reloadData];
}


-(NSMutableArray *) barList{
    theauthors = [[NSMutableArray alloc] init];
    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"TulsaBars.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"An error has occured: %@", sqlite3_errmsg(db));

        }


        const char *sql = "SELECT * FROM  TulsaBars";
        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
        }else{

            while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
                Author * author = [[Author alloc] init];
                author.barName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
                author.barAddress = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
                author.barState = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 5)];
                author.barLat = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 8)];
                author.barLong = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 9)];

                if (currentLoc == nil) {
                    NSLog(@"current location is nil %@", currentLoc);
                }else{

                CLLocation *barLocation = [[CLLocation alloc] initWithLatitude:[author.barLat doubleValue] longitude:[author.barLong doubleValue]];
                CLLocationDistance distancekm = [currentLoc distanceFromLocation: barLocation]/1000;
                NSString *distanceString = [[NSString alloc] initWithFormat: @"%f", distancekm];
                author.cachedDist = distanceString;

                [theauthors addObject:author];

                }
            }
        }
        sqlite3_finalize(sqlStatement);
    }
    @catch (NSException *exception) {
        NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
    }
    @finally {
        sqlite3_close(db);

        return theauthors;
    }
}


-(NSArray *) sortList{

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"cachedDist"  ascending:YES];
    sortedArray = [theauthors sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

    return sortedArray;
}
4

1 回答 1

0

嗯...您的-sortList方法没有进行就地排序,它返回一个排序数组,您将其丢弃。

-barList实际上,您的方法也是如此。

编辑:这并不完全正确。 -barList实际上是在反复覆盖一个 ivar,尽管尚不清楚这是否是故意的。

这样做的结果是:

[self barList];
[self sortList];

除了旋转一些 CPU 周期之外什么都不做——你没有对它们的返回值做任何事情!

编辑:好的,我看到您的排序(巧妙地)设置了一个 ivar,但它令人困惑,因为它还返回一个排序数组。请参阅下面的评论。

(这看起来也是一种对地理数据进行排序的过于复杂的方法,但我现在不打算遍历所有代码。您需要在查看之前修复上述问题。)

于 2012-07-02T23:26:05.860 回答