【问题标题】:iOS Objective C iBeacon Sorted UITable ViewiOS Objective C iBeacon 排序 UITable 视图
【发布时间】:2014-12-11 02:55:58
【问题描述】:

我目前正在编写一个小的 iBeacon 应用程序,我想列出所有发现的信标,按它们的接近程度排序,例如近、近、远。

当它们被发现时,它们会根据它们的邻近状态添加到表格视图中。

UI 表格视图将显示主要和次要信标,直接信标将位于顶部,远信标位于底部。

我可以将信标动态添加到表中没有问题,但是在发现信标时对数据进行排序时遇到问题。

如果数组中包含所有信标,我可以使用排序描述符对其进行排序没有问题,问题是当信标从远到近或立即打开新信标时,它总是以表格的底部。

我猜我可能应该使用:

insertRowsAtIndexPaths: withRowAnimation

但 IO 不知道我现在会怎么做。

非常感谢任何帮助。

谢谢。

【问题讨论】:

    标签: arrays uitableview sorting ibeacon


    【解决方案1】:

    每次将信标添加到可见列表时,您需要重新排序信标数组,然后调用[tableView reloadData]。下面是一个方法中的排序示例,该方法旨在在每个测距回调中被调用。

    - (void) updateIBeacons:(NSArray *)iBeaconsSeen {
        _firstRangingUpdateReceived = YES;
        NSDate * now = [NSDate date];
        __block BOOL resortNeeded = NO;
        __block BOOL replaced = NO;
        NSMutableArray *newSortedIBeacons = [[NSMutableArray alloc] initWithArray:[_sortedIBeacons copy]];
        [iBeaconsSeen enumerateObjectsUsingBlock:^(CLBeacon * iBeacon, NSUInteger idx, BOOL *stop) {
            replaced = NO;
            [_sortedIBeacons enumerateObjectsUsingBlock:^(CLBeacon * existingIBeacon, NSUInteger idx, BOOL *stop) {
                if ([existingIBeacon.proximityUUID isEqual:iBeacon.proximityUUID]  &&
                    [existingIBeacon.major isEqual:iBeacon.major] &&
                    [existingIBeacon.minor isEqual:iBeacon.minor]) {
                    [newSortedIBeacons setObject:iBeacon atIndexedSubscript:idx];
                    replaced = YES;
                }
            }];
            [_iBeaconLastSeenTime setObject:now forKey: iBeacon];
            if (!replaced) {
                resortNeeded = YES;
                [newSortedIBeacons addObject: iBeacon];
            }
    
        }];
    
        // now remove any iBeacons that have not been seen in the last 5 seconds
        for (long i = newSortedIBeacons.count-1; i >= 0; i--) {
            NSDate * lastSeen = (NSDate *) [_iBeaconLastSeenTime objectForKey:[newSortedIBeacons objectAtIndex:i]];
            if ([now timeIntervalSinceDate:lastSeen] > 5) {
                [newSortedIBeacons removeObjectAtIndex:i];
                resortNeeded = YES;
            }
        }
    
        if (resortNeeded) {
            NSLog(@"We need to sort again");
            NSArray *sortedArray = [newSortedIBeacons sortedArrayUsingComparator:^(CLBeacon* obj1, CLBeacon* obj2) {
                NSString *p1 = [NSString stringWithFormat:@"%@_%@_%@", [obj1.proximityUUID UUIDString], obj1.major, obj1.minor];
                NSString *p2 = [NSString stringWithFormat:@"%@_%@_%@", [obj2.proximityUUID UUIDString], obj2.major, obj2.minor];
                return [p1 compare:p2];
            }];
            _sortedIBeacons = [[NSMutableArray alloc] initWithArray:sortedArray];
        }
        else {
            _sortedIBeacons = newSortedIBeacons;
        }
    
        [self.tableView reloadData];
    
    }
    

    【讨论】:

      【解决方案2】:

      在tableview的datasource中添加新发现的beacon的方法中,可以先对beacon数组进行排序,然后[tableView reloadData]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-24
        • 2023-03-26
        相关资源
        最近更新 更多