Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableView中将核心数据排序为多个关系_Ios_Uitableview_Core Data_Nsfetchedresultscontroller_Nsorderedset - Fatal编程技术网

Ios 在UITableView中将核心数据排序为多个关系

Ios 在UITableView中将核心数据排序为多个关系,ios,uitableview,core-data,nsfetchedresultscontroller,nsorderedset,Ios,Uitableview,Core Data,Nsfetchedresultscontroller,Nsorderedset,我在这里看到了很多关于这个问题的帖子,但似乎没有一个能解决我的问题 我有一个使用核心数据的应用程序。我正在尝试对一个连接到NSFetchedResultsController的tableView进行排序,这比本文中的其他文章更复杂的是,我需要根据用户位置更新排序 我的设想如下: 我有一个实体叫做商店。商店有许多分支机构,每个分支机构都有自己的lat/long。我有一个表视图,显示选定商店的所有分支。我想要的是,当用户在tableview可见的情况下更新其位置时,单元格会更新其位置,以便table

我在这里看到了很多关于这个问题的帖子,但似乎没有一个能解决我的问题

我有一个使用核心数据的应用程序。我正在尝试对一个连接到NSFetchedResultsController的tableView进行排序,这比本文中的其他文章更复杂的是,我需要根据用户位置更新排序

我的设想如下:

我有一个实体叫做商店。商店有许多分支机构,每个分支机构都有自己的lat/long。我有一个表视图,显示选定商店的所有分支。我想要的是,当用户在tableview可见的情况下更新其位置时,单元格会更新其位置,以便tableview按顶部最近的分支排序

当用户移动到分支附近时,我可以通过单元格动画上下移动,使所有这些都正常工作。然而,我的解决方案不仅效率低下,而且当tableview中的单元格数量超过屏幕所能容纳的数量时,还存在一个重大缺陷

我知道最可能的方法是使用SensorDeredSet对我的商店实体上的多对多关系进行排序。但不确定如何实现这一点,因为我正在排序的值不是存储在核心数据中,而是动态计算的(如果有意义的话)。将存储lat和long,但会动态计算距离,其值是确定排序的距离值

目前,我使用CLLocationManger协议LocationManager:didUpdateLocations调用updateCellSortingOrder,这是我的排序方法。位置管理器设置为:

self.manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
我的排序方法有效,但正如我所说,它并不理想。我希望能够利用尽可能多的核心数据工具,如NSFetchedViewController、谓词、聚合等,而不是创建数组,对它们进行排序,然后将它们推回到FRC等,这就是我现在正在做的。。。。太乱了

这是我的代码,注意:branchCell是UITableViewCell的一个子类,它有一个包含分支的属性init。分支是我的实体分支的一个实例

//Creating the branch cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyTableCellView";

BranchCell *cell  = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[BranchCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


    Branch *currentBranch =  [[self.list.listsToDelivery allObjects] objectAtIndex:indexPath.row];

    cell.branch = currentBranch;
    cell.textLabel.text = currentBranch.title;
}

return cell;
}


如果有人面临与我相同的问题,这里是我的解决方案。前一段时间已修复,但从未更新。它已在我的应用程序中使用,该应用程序已在应用程序商店中交付:

updateCellSortingOrder在映射委托方法mapView:didUpdateUserLocation:

作废updateCellSortingOrder {

[self.myTableView重新加载数据]

[self.sortedCellary sortUsingComparator:^Delivery*Delivery A,Delivery*Delivery B {

}])

}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //update the cells
    [self updateCellSortingOrder];
}

- (void) updateCellSortingOrder
{
    // get the number of cells
    static int numberOfCells;

    numberOfCells = [[self.currentStore.branches allObjects]  count];

//loop though all the cells
    for (int cellAIndex = 0; cellAIndex < numberOfCells; cellAIndex++)
{
    //set up the cells
    static BranchCell * cellA;
    static BranchCell * cellB;

    //branch instances
    static Branch *branchA;
    static Branch *branchB;

    //distances
    static CLLocationDistance distanceA;
    static CLLocationDistance distanceB;
    static CLLocation *locationA;
    static CLLocation *locationB;

    //second loop to get the next cell 
    for (int cellBIndex = cellAIndex+1; cellBIndex < numberOfCells; cellBIndex++)
    {
        //assign the cells
        cellA = (BranchCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:cellAIndex inSection:0]];
        cellB = (BranchCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:cellBIndex inSection:0]];

        //get the two branches which we need to compate
        branchA = cellA.branch;
        branchB = cellB.branch;

        //calculate the distance
        locationA = [[CLLocation alloc] initWithLatitude:branchA.address.region.center.latitude longitude:branchA.address.region.center.longitude];

        locationB = [[CLLocation alloc] initWithLatitude:branchB.address.region.center.latitude longitude:branchB.address.region.center.longitude];

        distanceA = [self.manager.location distanceFromLocation:locationA];
        distanceB = [self.manager.location distanceFromLocation:locationB];

        //move the cell a to cell b's location if a's distances is greater than b's
        if (distanceA > distanceB)
        {
            [self.myTableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:cellAIndex inSection:0] toIndexPath:[NSIndexPath indexPathForRow:cellBIndex inSection:0]];

        } 
    }
}
}
 CLLocationDistance distanceA;
 CLLocationDistance distanceB;


 distanceA = [[self.mapView userLocation] distanceFromLocation: deliveryA.address];
 distanceB = [[self.mapView userLocation] distanceFromLocation: deliveryB.address];

 if ( distanceA > distanceB)
 {

     return (NSComparisonResult)NSOrderedDescending;
 }

 return (NSComparisonResult)NSOrderedSame;