如何在截面表视图中排序距离?

时间:2013-04-21 17:48:06

标签: objective-c uitableview sorting

我正在使用section tableview制作应用程序。节标题通过keyArray显示区域。对于表格视图的行,它将显示包括名称,地址和距离在内的所有信息。我可以通过以下代码来实现。

现在,我将在tableview中对升序距离进行排序。 如何在'cellForRowAtIndexPath'方法中对其进行排序?

我的第二个想法是在cellForRowAtIndexPath方法的外部创建NSArray。一种是加载plist数据并计算距离。然后,通过NSSortDescriptor对其进行排序。如果我使用这种方法,我不确定如何正确填充部分tableview?

有人可以给我一些想法或建议吗?

在key数组中,我使用下面的代码在ViewDidLoad中创建它并将其放入section header。

     //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;
    }

1 个答案:

答案 0 :(得分: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;
}

对于您的排序问题,我想回到您的dataDictionary。遍历每个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;
}

总空气规范,结果证明比我讨价还价更多。语法中可能存在的错误以及什么不是,但这是我为您的目的所看到的基本想法。