iDevelopper / PBRevealViewController

A UIViewController subclass for revealing a left and/or right view controller above or below a main view controller.
MIT License
80 stars 16 forks source link

How to create dynamic menu items in swrevealviewcontroller objective c #81

Closed ozamihir1990 closed 3 years ago

ozamihir1990 commented 4 years ago

Hello @iDevelopper You helped me a lot. So, if you could help me in this scenario, it will be very helpful for me. So I have 5 menus A, B, C, D, E, F, And based on the permission I need to show that menus. For e.g. If the user doesn't have permission for B and E then the Visible menus will be A, C, D, F. Right now I have used static arrays to show the menus and in the storyboard, I have added 5 menus statically. I can not use multiple arrays because I am showing around 10 menu items. Navigation I have done from the storyboard itself. Please see my code below.

#pragma mark -
#pragma mark - UITableViewDataSoruce and UITableViewDelegate
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kArrMenuImages[indexPath.section]];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kArrMenuImages[indexPath.row]] ;
    }
    if ([[kArrMenuImages objectAtIndex:indexPath.section] isEqualToString:@"Settings"]) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *data = [defaults objectForKey:@"UploadData"];
        if (data !=nil) {
            NSMutableArray *array1 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            int count = (int)[array1 count];
            if (count == 0) {
                //NSLog(@"No Data");
            } else {
                UploadQueue.instance.count = [array1 count];
                [[NSNotificationCenter defaultCenter] postNotificationName:UploadCountChangedNotification object:nil];
            }
            [self updateTableViewCell:cell forCount:count];
        }
    }
    if ([CommonUtil isDeviceLanguageRTL]) {
        cell.textLabel.textAlignment = NSTextAlignmentRight;
    }
    cell.textLabel.text = [kArrMenuTitles objectAtIndex:indexPath.section];
    cell.imageView.image = [UIImage imageNamed:[kArrMenuImages objectAtIndex:indexPath.section]];

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [kArrMenuImages count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5.0; // you can have your own choice, of course
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
- (void)updateTableViewCell:(UITableViewCell *)cell forCount:(NSUInteger)count {
    // Count > 0, show count
    if (count > 0) {

        // Create label
        CGFloat fontSize = 14;
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:fontSize];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor redColor];

        // Add count to label and size to fit
        label.text = [NSString stringWithFormat:@"%@", @(count)];
        [label sizeToFit];

        // Adjust frame to be square for single digits or elliptical for numbers > 9
        CGRect frame = label.frame;
        frame.size.height += (int)(0.4*fontSize);
        frame.size.width = (count <= 9) ? frame.size.height : frame.size.width + (int)fontSize;
        label.frame = frame;

        // Set radius and clip to bounds
        label.layer.cornerRadius = frame.size.height/2.0;
        label.clipsToBounds = true;

        // Show label in accessory view and remove disclosure
        cell.accessoryView = label;
        cell.accessoryType = UITableViewCellAccessoryNone;
        dispatch_async(dispatch_get_main_queue(), ^{ [self.tblMenu reloadData]; });
    }
    // Count = 0, show disclosure
    else {
        cell.accessoryView = nil;
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 8) {
         [self apiCallGetChatToken];
    } 
}

#pragma mark -
#pragma mark - Other Methods
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"Logout"]) {
        if ([self isLoggedIn]) {
            if ([CommonUtil isiPad]) {
                ViewController *loginController = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil] instantiateViewControllerWithIdentifier:@"login"]; //or the homeController
                [[UIApplication sharedApplication].keyWindow setRootViewController:loginController];
            } else {
                ViewController *loginController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"login"]; //or the homeController
                [[UIApplication sharedApplication].keyWindow setRootViewController:loginController];
            }
            [kNSUserDefaults synchronize];
            [Notifications unregisterDevice:^(bool suceeded){ NSLog(@"Logout Notification Deregister %s", suceeded? "true":"false"); }];
        }
    }
}

Please review and let me know if chances to do that.

Thanks in Advance.