Skip to content

Instantly share code, notes, and snippets.

@nfarah86
Last active May 4, 2016 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfarah86/17f6ac696aae33c7659427fa4054ba88 to your computer and use it in GitHub Desktop.
Save nfarah86/17f6ac696aae33c7659427fa4054ba88 to your computer and use it in GitHub Desktop.
NFHBeanListViewController Code Description
- (void)viewDidLoad {
[super viewDidLoad];
self.beansForIdentifiers = [NSMutableDictionary dictionary]; // initialize data structure
self.beanManager = [[PTDBeanManager alloc] initWithDelegate:self]; // create an instance of a Bean Manager
[self performSelector:@selector(scanForBeans) withObject:nil afterDelay:0.5]; //Start scanning for Beans
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login"
style:UIBarButtonItemStylePlain
target:self
action:@selector(showLoginView:)];
}
// some code omitted
- (void)scanForBeans
{
// I create a method that implements the SDK's startScanningForBeans_error
NSError *error = nil;
[self.beanManager startScanningForBeans_error:&error];
if (error)
{
NSLog(@"Error occurred while scaning for beans: %@", error);
}
}
- (void)beanManager:(PTDBeanManager*)beanManager didDiscoverBean:(PTDBean*)bean error:(NSError*)error
{
// SDK's method that checks to see if there are any nearby Beans
// for each bean that is discovered, add them to the dictionary
self.beansForIdentifiers[bean.identifier] = bean;
[self refreshBeanList];
}
// some code omitted
- (void)refreshBeanList
{
// I just sort the Beans by name from the dictionary and save it to an array
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
self.orderedBeans = [[self.beansForIdentifiers allValues] sortedArrayUsingDescriptors:@[sortByName]];
[self.tableView reloadData];
}
// some omitted code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// We write each element in the sorted array to the cell
static NSString *kCellIdentifier = @"BeanCell";
PTDBean *beanForRow = [self.orderedBeans objectAtIndex:indexPath.row];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"BeanCell"];
}
cell.textLabel.text = beanForRow.name;
cell.detailTextLabel.text = [beanForRow.identifier UUIDString];
return cell;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment