[英]Sorting core data to-many relationship in UITableView
我在这里看到了许多有关此问题的信息,但似乎都无法解决我遇到的问题。
我有一个使用核心数据的应用程序。 我正在尝试对连接到NSFetchedResultsController的tableView进行排序,这使该问题比此处的其他帖子更复杂的是,我需要根据用户位置更新排序。
我的情况如下:
我有一个称为商店的实体。 商店有很多分支,每个分支都有自己的经/纬度。 我有一个tableview,显示选定商店的所有分支。 我想要的是,当用户在查看表格视图的同时更新其位置时,单元格会更新其位置,以便该表格视图在顶部具有最接近的分支。
当用户靠近分支等时,我设法使单元动画上下移动,从而使所有这些工作正常进行。但是,我的解决方案不仅效率低下,而且当tableview的单元格数量超过可以容纳的范围时,它也存在一个重大错误。适合在屏幕上。
我知道最可能发生的故障是使用NSOrderedSet对我的Store实体上的多对多关系进行排序。 但是不知道如何实现它,因为我排序的值没有存储在核心数据中,而是在有意义的情况下进行了动态计算。 纬度和经度已存储,但距离是即时计算的,其距离值决定了排序。
目前,我使用CLLocationManger协议locactionManager: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;
}
-(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]];
}
}
}
}
如果有人遇到我遇到的相同问题,这是我的解决方法。 修复了一段时间,但从未更新。 它已在我的应用程序“已交付”中使用,该应用程序现在在应用程序商店中:)
在地图委托方法mapView:didUpdateUserLocation上调用了updateCellSortingOrder:
(无效)updateCellSortingOrder {
[self.myTableView reloadData];
[self.sortedCellArray sortUsingComparator:^(交付*交付A,交付*交付B){
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;
}];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.