2013-04-21 63 views
0

我在做應用程序通過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; 
    } 
+0

我注意到你的名字和地址字典都引用了來自sectionHeaderArray的完全相同的對象。 – 2013-04-21 18:25:30

+0

問題是什麼?只是在創建時對外部數組進行排序。 – DanSkeel 2013-04-21 19:19:19

回答

0

我跳到結論併發布了一個有缺陷的答案。我現在看到你的流程中有很多問題。

分配相同的對象,[sectionHeaderArray objectAtIndex:indexPath.row],幾個不同的變量,並期望從每個不同的結果。首先,我想作以下修改現有的代碼:

NSString*sectionHeader=[keyArray objectAtIndex:indexPath.section]; 

    NSArray *locationsForSection = [self.dataDictionary objectForKey:sectionHeader]; 

    NSDictionary *thisLocationInfo = [locationsForSection objectAtIndex:indexPath.row]; 
    float thisLat = [[thisLocationInfo objectForKey:@"latitude"] floatValue]; 
    float thisLong = [[thisLocationInfo objectForKey:@"longitude"] floatValue]; 

    //location info draw from plist 
    CLLocation *targetLocation = [[CLLocation alloc] initWithLatitude:thisLat longitude:thisLong]; 

    double distance = [self.myLocation distanceFromLocation:targetLocation]/1000; 

    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell==nil) { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier]; 
    } 
    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]; 

    latLabel1.text=[NSString stringWithFormat:@"%f",thisLat]; 
    longLabel1.text=[NSString stringWithFormat:@"%f",thisLong]; 

    disLabel.text=[NSString stringWithFormat:@"%0.2f km",distance]; 

    return cell; 
} 

爲了您的排序問題,我會說要回去你的數據字典。遍歷每個sectionHeader數組。將每個陣列替換爲按myLocation距離排序的副本。您可以將鍵和距離添加到每個字典中,然後將其完全從cellForRowAtIndexPath方法中取出。

-(NSDictionary *)dataDictionary 
{ 
    if (_dataDictionary) { 
     return _dataDictionary; 
    } 

    NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc] init]; 
    for (NSString *sectionHead in self.keyArray) { 
     NSMutableArray *sectionArrayWithDistances = [[NSMutableArray alloc] init]; 
     NSArray *thisSectionArray = [sourceDictionary objectForKey:sectionHead]; 
     for (NSDictionary *theLocationDict in thisSectionArray) { 
     // Calculate the distance 
     float thisLat = [[theLocationDict objectForKey:@"latitude"] floatValue]; 
     float thisLong = [[theLocationDict objectForKey:@"longitude"] floatValue]; 
     CLLocation *targetLocation = [[CLLocation alloc] initWithLatitude:thisLat longitude:thisLong]; 
     double distance = [self.myLocation distanceFromLocation:targetLocation]/1000; 

     // Insert modified dictionary into the resulting section array 
     NSMutableDictionary *thisLocationWithDistance = [NSMutableDictionary dictionaryWithDictionary:theLocationDict]; 
     [thisLocationWithDistance insertObject:[NSNumber numberWithDouble:distance] ForKey:@"distance"]; 
     [sectionArrayWithDistances addObject:[NSDictionary dictionaryWithDictionary:thisLocationWithDistance]]; 
     } 
     // Sort the resulting array for this section and insert in dataDict 
     NSSortDescriptor *sortByDistance = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES]; 
     NSArray *sortDescriptors = [NSArray arrayWithObjects:sortByDistance, nil]; 
     [sectionArrayWithDistances sortWithDescriptors:sortDescriptors]; 

     [resultDictionary insertObject:[NSArray arrayWithArray:sectionArrayWithDistances] forKey:sectionHead]; 
    } 

    // Set result and return 
    self.dataDictionary = [NSDictionary dictionaryWithDictionary:resultDictionary]; 
    return _dataDictionary; 
} 

總空氣代碼和原來是比我討價還價。在語法上可能存在的錯誤以及什麼不是,但這是我看到它的基本想法。

+0

感謝您的指導。我會嘗試。 – 2013-04-22 16:06:34

相關問題