[英]how to rearrange tableview after i get min location from current location in iphone sdk?
我創建了一個顯示用戶當前位置數據的應用程序。 我想根據用戶找到的最小距離按順序對它們進行排序。 我知道我需要使用這種 function:
[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;
提前致謝!
對 model 中的數組進行排序,使其處於所需的距離順序,然后在 tableview 上調用重新加載。 然后將從數據 model 刷新,您的列表將按正確的順序排列。
我找到了使用此代碼的解決方案,它將完美運行
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的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.