2
\$\begingroup\$

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 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.

I would like someone to tell me if 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:@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:@"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:@"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:@"Loading Staff"];
    staff = [_staffRef child:@"Staff"];
    [staff observeEventType:FIRDataEventTypeValue
                  withBlock:^(FIRDataSnapshot *snapshot) {
                    NSDictionary *valueDict = [[NSDictionary alloc] init];
                    for (snapshot in snapshot.children) {
                        valueDict = snapshot.value;
                        if ([valueDict[@"Position"] isEqualToString:@"Minister"]) {
                            [_ministerArray addObject:snapshot.value];
                        } else if ([valueDict[@"Position"] isEqualToString:@"Pastor"]) {
                            [_pastorArray addObject:snapshot.value];
                        } else if ([valueDict[@"Position"] isEqualToString:@"Worship & Creative Arts Director"]) {
                            [_directorArray addObject:snapshot.value];
                        } else if ([valueDict[@"Position"] isEqualToString:@"Financial Advisor"]) {
                            [_financeArray addObject:snapshot.value];
                        } else if ([valueDict[@"Position"] isEqualToString:@"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:@"staffCell" forIndexPath:indexPath];
    FIRDataSnapshot *snapshot = (array)[indexPath.row];
    cell.fullNameLabel.text = [snapshot valueForKey:@"fullName"];
    cell.positionLabel.text = [snapshot valueForKey:@"Position"];
    cell.emailTextView.text = [snapshot valueForKey:@"Email"];
    cell.phoneTextView.text = [snapshot valueForKey:@"Phone"];
    NSString *url = [snapshot valueForKey:@"imageURL"];
    [cell.staffImageView sd_setImageWithURL:[NSURL URLWithString:url]
                           placeholderImage:[UIImage imageNamed:@"imagePlaceholder.jpg"]
                                    options:SDWebImageRefreshCached];
}

@end
\$\endgroup\$
  • \$\begingroup\$ I don't use FireBase, so I don't know, but could you do something like that (in pseudo code): NSMutableArray *allData = alloc/init; for (int i = 0 to 5){[allData addObject:[NSMutableArray new]];} numberOfRowsInSection: allData[section].count; cellForRowAtIndexPath: [self registerSnapshotForArray:[allData[indexPath.section] atIndexPath:indexPath] or things like that? \$\endgroup\$ – Larme May 11 '17 at 15:48
0
\$\begingroup\$

First of all you are getting single response with all staff detail in single dictionary so

Why you are filtering in different array ?

Suggestion : You can use filtering on single dictionary with all sub array in it and access by position and show its in your tableView.

Structure of Dictionary is :

{
   @"Minister":[<Your Minister array>],
   @"Pastor":[<Your Pastor>],
   @"Worship & Creative Arts Director":[<Your Worship & Creative Arts Director Array">],
   @"Financial Advisor":[<Your Financial Advisor array>],
   @"Community Group Organizer":[<Your Community Group Organizer array>],

}

And based on this you can design your code for table View sections and Cells.

Hope this will helps how we can use minimal Array Container and use it to display data.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.