0
我無法將我的代碼從xib格式轉換爲我的iBeacon應用程序的故事板。表格單元格上沒有任何顯示。所有幫助表示讚賞!從xib轉換爲故事板
#import "RangingViewController.h"
#import "Default.h"
@implementation RangingViewController
{
NSMutableDictionary *_beacons;
CLLocationManager *_locationManager;
NSMutableArray *_rangedRegions;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if(self)
{
_beacons = [[NSMutableDictionary alloc] init];
// This location manager will be used to demonstrate how to range beacons.
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
// CoreLocation will call this delegate method at 1 Hz with updated range information.
// Beacons will be categorized and displayed by proximity.
[_beacons removeAllObjects];
NSArray *unknownBeacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity = %d", CLProximityUnknown]];
if([unknownBeacons count])
[_beacons setObject:unknownBeacons forKey:[NSNumber numberWithInt:CLProximityUnknown]];
NSArray *immediateBeacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity = %d", CLProximityImmediate]];
if([immediateBeacons count])
[_beacons setObject:immediateBeacons forKey:[NSNumber numberWithInt:CLProximityImmediate]];
NSArray *nearBeacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity = %d", CLProximityNear]];
if([nearBeacons count])
[_beacons setObject:nearBeacons forKey:[NSNumber numberWithInt:CLProximityNear]];
NSArray *farBeacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity = %d", CLProximityFar]];
if([farBeacons count])
[_beacons setObject:farBeacons forKey:[NSNumber numberWithInt:CLProximityFar]];
[self.rangeTable reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{
// Start ranging when the view appears.
[_rangedRegions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CLBeaconRegion *region = obj;
[_locationManager startRangingBeaconsInRegion:region];
}];
}
- (void)viewDidDisappear:(BOOL)animated
{
// Stop ranging when the view goes away.
[_rangedRegions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CLBeaconRegion *region = obj;
[_locationManager stopRangingBeaconsInRegion:region];
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Ranging";
// Populate the regions we will range once.
_rangedRegions = [NSMutableArray array];
[[Default sharedDefaults].supportedProximityUUIDs enumerateObjectsUsingBlock:^(id uuidObj, NSUInteger uuidIdx, BOOL *uuidStop) {
NSUUID *uuid = (NSUUID *)uuidObj;
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:[uuid UUIDString]];
[_rangedRegions addObject:region];
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)rangeTable
{
return _beacons.count;
}
- (NSInteger)tableView:(UITableView *)rangeTable numberOfRowsInSection:(NSInteger)section
{
NSArray *sectionValues = [_beacons allValues];
return [[sectionValues objectAtIndex:section] count];
}
- (NSString *)tableView:(UITableView *)rangeTable titleForHeaderInSection:(NSInteger)section
{
NSString *title = nil;
NSArray *sectionKeys = [_beacons allKeys];
// The table view will display beacons by proximity.
NSNumber *sectionKey = [sectionKeys objectAtIndex:section];
switch([sectionKey integerValue])
{
case CLProximityImmediate:
title = @"Immediate";
break;
case CLProximityNear:
title = @"Near";
break;
case CLProximityFar:
title = @"Far";
break;
default:
title = @"Unknown";
break;
}
return title;
}
- (UITableViewCell *)tableView:(UITableView *)rangeTable cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
_rangeCell = [rangeTable dequeueReusableCellWithIdentifier:identifier];
if (_rangeCell == nil)
{
_rangeCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
_rangeCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Display the UUID, major, minor and accuracy for each beacon.
NSNumber *sectionKey = [[_beacons allKeys] objectAtIndex:indexPath.section];
CLBeacon *beacon = [[_beacons objectForKey:sectionKey] objectAtIndex:indexPath.row];
_rangeCell.textLabel.text = [beacon.proximityUUID UUIDString];
_rangeCell.detailTextLabel.text = [NSString stringWithFormat:@"Major: %@, Minor: %@, Acc: %.2fm", beacon.major, beacon.minor, beacon.accuracy];
return _rangeCell;
}
@end
我已經將TableViewCell聲明爲_rangeCell,將TableView聲明爲rangeView。
您是否在故事板中將Cell Identifier設置爲「Cell」?請檢查一次。 – cjd
您是否已將IBOutlet屬性連接到故事板中的對應部分? –
我確實勾住了它們,但它仍然無法使用。 – user3161723