显示时更新 RESideMenu 中的 tableview 数据

Posted

技术标签:

【中文标题】显示时更新 RESideMenu 中的 tableview 数据【英文标题】:Update tableview data in RESideMenu when it showing 【发布时间】:2016-04-05 11:39:50 【问题描述】:

我在右侧菜单中使用UITableView,但我找不到RESideMenu 的任何功能,它跟踪该菜单正在显示(或将显示)。

我发现了两个函数:

- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController

- (void)sideMenu:(RESideMenu *)sideMenu didShowMenuViewController:(UIViewController *)menuViewController

但他们没有做我需要的。 菜单打开时如何更新我的表格视图? 我的代码:RightMenuViewController.h

#import <UIKit/UIKit.h>
#import "DataAPI.h"
#import "RESideMenu.h"
#import "RightMenuTableViewCell.h"

@interface RightMenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, RESideMenuDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

RightMenuViewController.m

#import "RightMenuViewController.h"

@interface RightMenuViewController ()

@end

@implementation RightMenuViewController

- (void)viewDidLoad 
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:[[[UICustomization sharedInstance].backgroundColour objectForKey:@"r"] floatValue]/225.0f green:[[[UICustomization sharedInstance].backgroundColour objectForKey:@"g"] floatValue]/225.0f blue:[[[UICustomization sharedInstance].backgroundColour objectForKey:@"b"] floatValue]/225.0f alpha:1.0f];

    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];

    if ([[UICustomization sharedInstance].tableBackgroundColour count])
        _tableView.backgroundColor = [UIColor colorWithRed:[[[UICustomization sharedInstance].tableBackgroundColour objectForKey:@"r"] floatValue]/225.0f green:[[[UICustomization sharedInstance].tableBackgroundColour objectForKey:@"g"] floatValue]/225.0f blue:[[[UICustomization sharedInstance].tableBackgroundColour objectForKey:@"b"] floatValue]/225.0f alpha:1.0f];
    else
        _tableView.backgroundColor = [UIColor clearColor];


- (void)viewDidAppear:(BOOL)animated 
    [super viewDidAppear:YES];
    NSLog(@"push notifications: %@", [DataAPI sharedInstance].pushNotifications);


- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController 
    [_tableView reloadData];
    NSLog(@"push notifications: %@", [DataAPI sharedInstance].pushNotifications);


- (void)sideMenu:(RESideMenu *)sideMenu didShowMenuViewController:(UIViewController *)menuViewController 
        [_tableView reloadData];
    NSLog(@"push notifications: %@", [DataAPI sharedInstance].pushNotifications);


- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    if (![[DataAPI sharedInstance].pushNotifications count])
        return 1;
    else
        return [[[DataAPI sharedInstance].pushNotifications objectForKey:@"data"] count];

    return 0;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return 1;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    if ([[DataAPI sharedInstance].pushNotifications count]) 
        [DataAPI sharedInstance].orderNavButtonsOn = YES;
        [[ConnectionService instance]get_single_order:[UserAuth instance].username password:[UserAuth instance].password orderID:[[[[DataAPI sharedInstance].pushNotifications objectForKey:@"data"] objectAtIndex:indexPath.section] objectForKey:@"objectID"]];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(receiveConnectionData:)
                                                     name:@"SingleOrderDataReceived"
                                                   object:nil];
    


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
    return 10.;


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    return nil;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    if (![[DataAPI sharedInstance].pushNotifications count]) 
        static NSString *MyIdentifier = @"Identifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

        if (cell == nil)
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];

        cell.layer.borderWidth = 1.0f;
        cell.layer.borderColor = [UIColor colorWithRed:[[[UICustomization sharedInstance].cellBorderColour objectForKey:@"r"] floatValue]/225.0f green:[[[UICustomization sharedInstance].cellBorderColour objectForKey:@"g"] floatValue]/225.0f blue:[[[UICustomization sharedInstance].cellBorderColour objectForKey:@"b"] floatValue]/225.0f alpha:1.0f].CGColor;

        cell.textLabel.text = NSLocalizedString(@"no_notifications", nil);
        cell.textLabel.textColor = [UIColor colorWithRed:[[[UICustomization sharedInstance].primaryTextColour objectForKey:@"r"] floatValue]/225.0f green:[[[UICustomization sharedInstance].primaryTextColour objectForKey:@"g"] floatValue]/225.0f blue:[[[UICustomization sharedInstance].primaryTextColour objectForKey:@"b"] floatValue]/225.0f alpha:1.0f];

        cell.userInteractionEnabled = NO;

        return cell;
     else 
        static NSString *MyIdentifier = @"rightMenuCell";
        RightMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

        if (cell == nil)
            cell = [[RightMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];

        cell.dateLabel.text = [[[[DataAPI sharedInstance].pushNotifications objectForKey:@"data"] objectAtIndex:indexPath.section] objectForKey:@"date_time"];
        cell.timeLabel.text = [[[[DataAPI sharedInstance].pushNotifications objectForKey:@"data"] objectAtIndex:indexPath.section] objectForKey:@"ago"];
        cell.statusLabel.text = [[[[DataAPI sharedInstance].pushNotifications objectForKey:@"data"] objectAtIndex:indexPath.section] objectForKey:@"pushed_message"];

        return cell;
    

    return 0;


- (void)receiveConnectionData:(NSNotification *) notification 
    if ([[notification name] isEqualToString:@"SingleOrderDataReceived"]) 
        [DataAPI sharedInstance].singleOrder = notification.object;
        dispatch_sync(dispatch_get_main_queue(), ^
            [self goToViewWithIdentifier:@"toOrder"];
        );
    


- (void)goToViewWithIdentifier:(NSString *)identifier 
    [self connectionCheck];
    [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:identifier]]
                                                 animated:YES];
    [self.sideMenuViewController hideMenuViewController];


- (void)connectionCheck 
    if ([[ConnectionService instance]checkConnectivity] == NO) 
        UIAlertController * alert = [UIAlertController
                                     alertControllerWithTitle:NSLocalizedString(@"internet_connection_error_title", nil)
                                     message:NSLocalizedString(@"internet_connection_error_message", nil)
                                     preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* quitButton = [UIAlertAction
                                     actionWithTitle:NSLocalizedString(@"internet_connection_error_cancel_button", nil)
                                     style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction * action)
                                     
                                         exit(0);
                                     ];

        UIAlertAction* tryAgainButton = [UIAlertAction
                                         actionWithTitle:NSLocalizedString(@"internet_connection_error_try_again_button", nil)
                                         style:UIAlertActionStyleDefault
                                         handler:^(UIAlertAction * action)
                                         
                                             [self connectionCheck];
                                         ];

        [alert addAction:quitButton];
        [alert addAction:tryAgainButton];

        [self presentViewController:alert animated:YES completion:nil];
    


@end

【问题讨论】:

你到底在做什么? 我正在从另一个视图控制器接收 tableview 的更新数据(位于右侧菜单中),当我打开右侧菜单时,我需要重新加载我的 tableview。 您使用的是哪种类型的更新数据?数组类型还是其他? 我正在使用 NSDictionary。 显示我的答案,我正在更新。我会知道这是否有用。 【参考方案1】:

签入 DEMOLeftMenuViewController ,以更新您的 tableview。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    return 54;


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex

    return 5;


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

    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
    

    NSArray *titles = @[@"Home", @"Calendar", @"Profile", @"Settings", @"Log Out"];
    NSArray *images = @[@"IconHome", @"IconCalendar", @"IconProfile", @"IconSettings", @"IconEmpty"];
    cell.textLabel.text = titles[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];

    return cell;

//委托//

- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController

[reload tableview];


【讨论】:

这是 uitableview 的一个简单示例,我知道如何使用它。并且在菜单中的演示表视图中更新。我需要弄清楚如何跟踪“菜单何时显示/正在显示”。 我在提问之前尝试过这样做。不工作。【参考方案2】:

结果比解决方案很简单:

- (void)viewDidAppear:(BOOL)animated 
    [super viewDidAppear:YES];
    [_tableView reloadData];

【讨论】:

以上是关于显示时更新 RESideMenu 中的 tableview 数据的主要内容,如果未能解决你的问题,请参考以下文章

使用 ResideMenu lib 时删除菜单项

在使用 RESideMenu 显示视图控制器之前,使用 AFNetworking 在 didFinishLaunching 中尽快检查可达性

RESideMenu:在 RESideMenu 之前添加新的视图控制器

ResideMenu 项禁用?

仅当与 table2 中的 column3 匹配时才更新 table1 中的 column1

使用 RESideMenu 移动到其他视图