从xib转换为storyboard

时间:2014-01-21 04:57:16

标签: ios objective-c xcode storyboard ibeacon

我无法将我的代码从xib格式转换为我的iBeacon应用程序的故事板。表格单元格上没有显示任何内容。感谢所有帮助!

#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。

1 个答案:

答案 0 :(得分:0)

在故事板中 -First创建一个没有任何子类的原型单元格 - 删除与对象的所有钩子链接 - 设置单元格标识符 - 使用下面的代码,让我知道问题是否仍然存在:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath{
   static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

  // Set the indexes according to your UIControls Listing     
   UILabel *lblMainText = (UILabel *)[cell.contentView.subviews objectAtIndex:0];
   UILabel *lblDetailText = (UILabel *)[cell.contentView.subviews objectAtIndex:3];
   UILabel *lblDetailText2 = (UILabel *)[cell.contentView.subviews objectAtIndex:1];
   UILabel *lblDetailText3 = (UILabel *)[cell.contentView.subviews objectAtIndex:2];
   UIButton *btnFavourite = (UIButton *)[cell.contentView.subviews objectAtIndex:6];
   UIButton *btnMore = (UIButton *)[cell.contentView.subviews objectAtIndex:4];
 //cell.backgroundColor = [UIColor clearColor];
lblMainText.text = [NSString stringWithFormat:@"%@",@"someValue"];
    lblDetailText.text= [NSString stringWithFormat:@"%@",@"someValue"];
   lblDetailText2.text= [NSString stringWithFormat:@"%@",@"someValue"];
   lblDetailText3.text= [NSString stringWithFormat:@"Marks",@"someValue"];
   btnFavourite.tag = indexPath.row;
  [btnFavourite addTarget:self
                      action:@selector(callMethod:) forControlEvents:UIControlEventTouchUpInside];
btnFavourite.tag = indexPath.row;
 return cell;
  }