如何在iphonesdk中从当前位置获取最小位置后重新排列表视图



我创建了一个应用程序,显示用户当前位置的数据。我想根据用户找到的最小距离,按组装顺序对它们进行排序。我知道我需要使用这种功能:

[userLocationd istanceFromLocation:cameraLocation];

但是,那之后我该怎么办?我应该根据距离对每个单元格进行排序,并将它们放在一个数组中,但我如何根据用户的最小位置重新排列每个单元格?(cellForRowAtIndexPath)

此外;我还没有真正弄清楚如何找到用户的位置。我同意这一点,但如果有人给我指明正确的方向,那会很有帮助。

如何将数组排序到最小距离到最大位置?

我用过这个代码。

    CLLocation *location1 = [[[CLLocation alloc] initWithLatitude:someLatitude longitude:someLongitude] autorelease];
    CLLocation *location2 = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    float target = [location2 distanceFromLocation:location1];
    target = target / 1000;

提前感谢!

对模型中的数组进行排序,使其按所需的距离顺序排列,然后在表视图上调用重载。然后,它将从数据模型中刷新,您的列表将按正确的顺序排列。

我已经找到了使用此代码的解决方案,它将完美地运行

 uint smallest[5];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithDouble:0.5], [NSNumber numberWithDouble:0.25], [NSNumber numberWithDouble:1], [NSNumber numberWithDouble:0.75], [NSNumber numberWithDouble:0.15], [NSNumber numberWithDouble:0.85], [NSNumber numberWithDouble:0.95], [NSNumber numberWithDouble:0.32], [NSNumber numberWithDouble:0.21], [NSNumber numberWithDouble:0.05], nil];
    // declare flags
    uint flags[array.count];
    // initialize flags
    for(uint i = 0; i < [array count]; i++)
        flags[i] = 0;
    for(int i = 0; i < 5; i++)
    {
        smallest[i] = -1;
        for(int j = 0; j < [array count]; j++)
        {
            // check flag
            if(!flags[j])
            {
                if(smallest[i] == -1 ||
                   [[array objectAtIndex:smallest[i]] doubleValue] > [[array objectAtIndex:j] doubleValue])
                {
                    // reset flag for previous index
                    if(smallest[i] != -1)
                        flags[smallest[i]] = 0;
                    // set flag for current index
                    flags[j] = 1;
                    smallest[i] = j;
                }
            }
        }
    }
    for(uint i = 0; i < 5; i++)
        NSLog(@"Smallest[%u]: %u -> %f", i, smallest[i], [[array objectAtIndex:smallest[i]] doubleValue]);

如果有人有任何问题,请链接到这个专门针对原始开发人员MatthewD 的问题

最新更新