我在做應用程序通過keyArray部分tableview.The節頭顯示區。對於一排表格視圖,它將顯示所有信息,包括名稱,地址和距離。我可以通過下面的代碼。現在如何在分段表視圖中對距離進行排序?
,我將上升距離的tableview排序。 如何在'cellForRowAtIndexPath'方法中對其進行排序?
我的第二個想法是的cellForRowAtIndexPath方法之外創建的NSArray。一個是加載plist數據並計算距離。然後,通過NSSortDescriptor進行分類。如果我去這個方法,我不知道如何正確地填充它在tableview部分?
有人可以給我一些想法或建議?
在鍵陣列,我使用以下代碼中viewDidLoad中創建它,放入節頭。
//location info draw from plist
NSArray*tempArray=[[NSArray alloc]init];
tempArray=[dataDictionary allKeys];
self.keyArray=[tempArray mutableCopy];
在的cellForRowAtIndexPath,它計算目標位置以及用戶位置之間的距離。然後在表格視圖行中顯示所有信息。
NSString*sectionHeader=[keyArray objectAtIndex:indexPath.section];
NSArray*sectionHeaderArray=[dataDictionary objectForKey:sectionHeader];
NSDictionary*targetLat=[sectionHeaderArray objectAtIndex:indexPath.row];
NSDictionary*targetLong=[sectionHeaderArray objectAtIndex:indexPath.row];
//location info draw from plist
CLLocation *targetLocation = [[CLLocation alloc] initWithLatitude:[[targetLat objectForKey:@"latitude"] floatValue] longitude:[[targetLong objectForKey:@"longitude"] floatValue]];
double distance = [self.myLocation distanceFromLocation:targetLocation]/1000;
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier] autorelease];
}
UILabel*nameLabel=(UILabel*)[cell viewWithTag:100];
UILabel*addLabel=(UILabel*)[cell viewWithTag:101];
UILabel*latLabel1=(UILabel*)[cell viewWithTag:102];
UILabel*longLabel1=(UILabel*)[cell viewWithTag:103];
UILabel*disLabel=(UILabel*)[cell viewWithTag:106];
NSDictionary*name=[sectionHeaderArray objectAtIndex:indexPath.row];
NSDictionary*address=[sectionHeaderArray objectAtIndex:indexPath.row];
nameLabel.text=[name objectForKey:@"name"];
addrLabel.text=[address objectForKey:@"address"];
latLabel1.text=[NSString stringWithFormat:@"%f",self.myLocation.coordinate.latitude];
longLabel1.text=[NSString stringWithFormat:@"%f",self.myLocation.coordinate.longitude];
disLabel.text=[NSString stringWithFormat:@"%0.2f km",distance];
return cell;
}
我注意到你的名字和地址字典都引用了來自sectionHeaderArray的完全相同的對象。 – 2013-04-21 18:25:30
問題是什麼?只是在創建時對外部數組進行排序。 – DanSkeel 2013-04-21 19:19:19