Question

I am new to xcode. i am trying to do a small project on ibeacon. Now the problem is I have included a tableview in my viewcontroller (i am using a tab view controller). I wanted to update the table once I have found a matching beacon with the major value i wanted.

With my code, once i got into the region and detected the beacon, I have received notification. However when I open that notification the table is not updated with the array i set.

#import "SBSFirstViewController.h"


@interface SBSFirstViewController ()


@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property  BOOL checkDidEnterBusStop;
@property (strong, nonatomic) CLBeacon *selectedBeacon;

@end

@implementation SBSFirstViewController


NSArray *buses;
BOOL checkDidEnterBusStop = NO;

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if(checkDidEnterBusStop == YES){
    return [buses count];
}
return 0;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:     (NSIndexPath *)indexPath{

static NSString *tableIdentifier =@"BusCell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];

if(cell==nil){

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];

}



cell.textLabel.text = [buses objectAtIndex:indexPath.row];



return cell;



}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

buses = [NSArray arrayWithObjects:@"3", @"83", @"62", nil];

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

[self initRegion];

[self.locationManager startMonitoringForRegion:_beaconRegion];
_beaconRegion.notifyOnEntry = YES;

NSLog(@"start monitoring");




}



-(void)initRegion
{
NSUUID *demoBusStopUUID = [[NSUUID alloc]initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];

self.beaconRegion = [[CLBeaconRegion alloc]initWithProximityUUID:demoBusStopUUID major:23118 minor:37538 identifier:@"estimoteAtBusStop"];



}




-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{

[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
NSLog(@"entered region");



}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{

[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}



-(void) locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons   inRegion:(CLBeaconRegion *)region {


checkDidEnterBusStop=YES;


UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.alertBody = @"You have reached the bus stop";
notification.soundName = UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication] presentLocalNotificationNow:notification];

}





- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}





@end

Can someone help me out by pointing out my mistakes. Thanks

Was it helpful?

Solution

You need to notify the tableview that the data has changed - call

[tableView reloadData] 

in your didRangeBeacons method - this will get it to call numberOfRowsInSection again

OTHER TIPS

After you receive the notification and update the data source you must call the reloadData method on the UITableView

[tableView reloadData];

I didn't find any object of UITableView

in .h file

@property (strong, nonatomic) UITableView *objTableView;

in .m file

@synthesize objTableView;

then in ViewDidLoadMethod

if(!objTableView)
 objTableView=[[objTableView alloc]initWithFrame:CGRectMake(10, 0, 300, 300)
                                             style:UITableViewStylePlain];
                        objTableView.delegate=self;
                       objTableView.dataSource=self;
                       [self.view addSubview:objTableView];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top