1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Code Populates a UITableView with Staff Members using Firebase Data

Discussion in 'Programming/Internet' started by Joshua Hart, Oct 8, 2018.

  1. Joshua Hart

    Joshua Hart Guest

    What this tableView accomplishes: The end user will be able to see a list of staff that works at a church. The staff consists of 5 individuals with 5 different position titles (Minister, Pastor, etc..). This data is pulled from Firebase.

    My concerns with my code: My code "works" correctly but I keep coming back to it with ambitions of making it better because I do not like my "if else" statements in the network request method of "[self getUpdates]". I don't like that I have to split the data into 5 separate NSMutableArrays and into one searchable NSDictionary.

    What I would like: Someone to tell me I'm doing it wrong and to recommend a cleaner and shorter approach.

    HEADER FILE:

    #import <UIKit/UIKit.h>
    @import Firebase;
    @import FirebaseDatabase;

    /**
    @abstract TableView that displays the working staff at the church.
    */
    @interface StaffTableViewController : UITableViewController
    /**
    @abstract Hamburger Menu
    */
    @property (strong, nonatomic) IBOutlet UIBarButtonItem *sideBarButton;

    /**
    @abstract FireDatabaseReference needed to do network call.
    */
    @property (strong, nonatomic) FIRDatabaseReference *staffRef;

    /**
    @abstract Array of Ministers that work at the church.
    */
    @property (strong, nonatomic) NSMutableArray *ministerArray;

    /**
    @abstract Array of Pastors that work at the church.
    */
    @property (strong, nonatomic) NSMutableArray *pastorArray;

    /**
    @abstract Array of Directors that work at the church.
    */
    @property (strong, nonatomic) NSMutableArray *directorArray;

    /**
    @abstract Array of Financial Advisors that work at the church.
    */
    @property (strong, nonatomic) NSMutableArray *financeArray;

    /**
    @abstract Array of Organizers that work at the church.
    */
    @property (strong, nonatomic) NSMutableArray *organizerArray;

    @end


    IMPLEMENTATION FILE

    #import "StaffTableViewController.h"
    #import "OurStaffTableViewCell.h"
    #import "SWRevealViewController.h"
    #import <ProgressHUD/ProgressHUD.h>
    #import <SDWebImage/UIImageView+WebCache.h>

    @interface StaffTableViewController ()

    @end

    FIRDatabaseReference *staff;

    @implementation StaffTableViewController

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.ministerArray = [NSMutableArray array];
    self.pastorArray = [NSMutableArray array];
    self.directorArray = [NSMutableArray array];
    self.financeArray = [NSMutableArray array];
    self.organizerArray = [NSMutableArray array];
    self.staffRef = [[FIRDatabase database] reference];
    [self setSideBarMenu];
    [self getUpdates];
    }

    - (void)setSideBarMenu {
    SWRevealViewController *revealViewController = self.revealViewController;
    if (revealViewController) {
    [_sideBarButton setTarget:revealViewController];
    [_sideBarButton setAction:mad:selector (revealToggle:)];
    [self.view addGestureRecognizer:revealViewController.panGestureRecognizer];
    }
    }

    #pragma mark - tableView data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
    return _ministerArray.count;
    }
    if (section == 1) {
    return _pastorArray.count;
    }
    if (section == 2) {
    return _directorArray.count;
    }
    if (section == 3) {
    return _financeArray.count;
    }
    if (section == 4) {
    return _organizerArray.count;
    }
    return 0;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OurStaffTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mad:"staffCell" forIndexPath:indexPath];
    switch (indexPath.section) {
    case 0:
    [self registerSnapshotForArray:_ministerArray atIndexPath:indexPath];
    break;
    case 1:
    [self registerSnapshotForArray:_pastorArray atIndexPath:indexPath];
    break;
    case 2:
    [self registerSnapshotForArray:_directorArray atIndexPath:indexPath];
    break;
    case 3:
    [self registerSnapshotForArray:_financeArray atIndexPath:indexPath];
    break;
    case 4:
    [self registerSnapshotForArray:_organizerArray atIndexPath:indexPath];
    break;
    default:
    return nil;
    }
    return cell;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 34;
    }

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake (0, 0, tableView.frame.size.width, 34)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake (10, 5, tableView.frame.size.width, 24)];
    [label setFont:[UIFont fontWithName:mad:"BebasNeueRegular" size:18]];
    label.textColor = [UIColor blackColor];
    [view
    setBackgroundColor:[UIColor colorWithRed:217.0f / 255.0f green:217.0f / 255.0f blue:217.0f / 255.0f alpha:1.0]];
    switch (section) {
    case 0:
    label.text = @"Minister";
    break;
    case 1:
    label.text = @"Pastor";
    break;
    case 2:
    label.text = @"Worship & Creative Arts Director";
    break;
    case 3:
    label.text = @"Financial Advisor";
    break;
    case 4:
    label.text = @"Community Group Organizer";
    break;
    default:
    break;
    }
    [view addSubview:label];
    return view;
    }

    #pragma mark - Network call to Firebase

    - (void)getUpdates {
    [ProgressHUD show:mad:"Loading Staff"];
    staff = [_staffRef child:mad:"Staff"];
    [staff observeEventType:FIRDataEventTypeValue
    withBlock:^(FIRDataSnapshot *snapshot) {
    NSDictionary *valueDict = [[NSDictionary alloc] init];
    for (snapshot in snapshot.children) {
    valueDict = snapshot.value;
    if ([valueDict[@"Position"] isEqualToString:mad:"Minister"]) {
    [_ministerArray addObject:snapshot.value];
    } else if ([valueDict[@"Position"] isEqualToString:mad:"Pastor"]) {
    [_pastorArray addObject:snapshot.value];
    } else if ([valueDict[@"Position"] isEqualToString:mad:"Worship & Creative Arts Director"]) {
    [_directorArray addObject:snapshot.value];
    } else if ([valueDict[@"Position"] isEqualToString:mad:"Financial Advisor"]) {
    [_financeArray addObject:snapshot.value];
    } else if ([valueDict[@"Position"] isEqualToString:mad:"Community Group Organizer"]) {
    [_organizerArray addObject:snapshot.value];
    }
    }
    [ProgressHUD dismiss];
    [self.tableView reloadData];
    }];
    }

    #pragma Helper Methods

    - (void)registerSnapshotForArray:(NSArray *)array atIndexPath:(NSIndexPath *)indexPath {
    OurStaffTableViewCell *cell =
    [self.tableView dequeueReusableCellWithIdentifier:mad:"staffCell" forIndexPath:indexPath];
    FIRDataSnapshot *snapshot = (array)[indexPath.row];
    cell.fullNameLabel.text = [snapshot valueForKey:mad:"fullName"];
    cell.positionLabel.text = [snapshot valueForKey:mad:"Position"];
    cell.emailTextView.text = [snapshot valueForKey:mad:"Email"];
    cell.phoneTextView.text = [snapshot valueForKey:mad:"Phone"];
    NSString *url = [snapshot valueForKey:mad:"imageURL"];
    [cell.staffImageView sd_setImageWithURL:[NSURL URLWithString:url]
    placeholderImage:[UIImage imageNamed:mad:"imagePlaceholder.jpg"]
    options:SDWebImageRefreshCached];
    }

    @end

    Login To add answer/comment
     

Share This Page