如何更新TableView静态单元格中的数据?

时间:2017-07-14 04:05:39

标签: ios objective-c uitableview tableview

我想更新计数值,但我无法做到这一点......!我试过了,但是当我构建代码时它会更新......!

以下是tableview的屏幕截图:

1

以下是此代码,

#import "LeftMenuViewController.h"
#import "RKDropdownAlert.h"
#import "HexColors.h"
#import "AppConstanst.h"
#import "GlobalVariables.h"
#import "MyWebservices.h"
#import <SDWebImage/UIImageView+WebCache.h>
@import Firebase;
@interface LeftMenuViewController (){
    NSUserDefaults *userDefaults;
    GlobalVariables *globalVariables;
    NSArray *numbers;
}
@end

@implementation LeftMenuViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self.slideOutAnimationEnabled = YES;

    return [super initWithCoder:aDecoder];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"Naaa-LeftMENU");


    self.tableView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];

}

-(void)viewWillAppear:(BOOL)animated{

    userDefaults=[NSUserDefaults standardUserDefaults];
    globalVariables=[GlobalVariables sharedInstance];
     NSLog(@"Role : %@",[userDefaults objectForKey:@"role"]);
    _user_role.text=[[userDefaults objectForKey:@"role"] uppercaseString];

    _user_nameLabel.text=[userDefaults objectForKey:@"profile_name"];
    _url_label.text=[userDefaults objectForKey:@"baseURL"];

    [_user_profileImage sd_setImageWithURL:[NSURL URLWithString:[userDefaults objectForKey:@"profile_pic"]]
                          placeholderImage:[UIImage imageNamed:@"default_pic.png"]];
    _user_profileImage.layer.borderColor=[[UIColor hx_colorWithHexRGBAString:@"#0288D1"] CGColor];

    _user_profileImage.layer.cornerRadius = _user_profileImage.frame.size.height /2;
    _user_profileImage.layer.masksToBounds = YES;
    _user_profileImage.layer.borderWidth = 0;



    NSInteger open =  [globalVariables.OpenCount integerValue];
    NSInteger closed = [globalVariables.ClosedCount integerValue];
    NSInteger trash = [globalVariables.DeletedCount integerValue];
    NSInteger unasigned = [globalVariables.UnassignedCount integerValue];
    NSInteger my_tickets = [globalVariables.MyticketsCount integerValue];

    if(open>999){
        _inbox_countLabel.text=@"999+";
    }else
        _inbox_countLabel.text=@(open).stringValue;
    if(closed>999){
        _closed_countLabel.text=@"999+";
    }else
        _closed_countLabel.text=@(closed).stringValue;
    if(trash>999){
        _trash_countLabel.text=@"999+";
    }else
        _trash_countLabel.text=@(trash).stringValue;
    if(unasigned>999){
        _unassigned_countLabel.text=@"999+";
    }else
        _unassigned_countLabel.text=@(unasigned).stringValue;
    if(my_tickets>999){
        _myTickets_countLabel.text=@"999+";
    }else
        _myTickets_countLabel.text=@(my_tickets).stringValue;

    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                             bundle: nil];

    // UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    UIViewController *vc ;

    switch (indexPath.row)
    {
        case 1:
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"CreateTicket"];
            break;

        case 2:
            [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
            break;
        case 3:
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"InboxID"];
            break;
        case 4:
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"MyTicketsID"];
            break;
        case 5:
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"UnassignedTicketsID"];
            break;
        case 6:
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"ClosedTicketsID"];
            break;

        case 7:
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"TrashTicketsID"];
            break;

        case 8:
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"ClientListID"];
            break;

        case 10:
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"AboutVCID"];
            break;


        case 11:

            [self wipeDataInLogout];
            //[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
            //[[SlideNavigationController sharedInstance] popToRootViewControllerAnimated:NO];

            [RKDropdownAlert title:@"Faveo Helpdesk" message:@"You've logged out, successfully." backgroundColor:[UIColor hx_colorWithHexRGBAString:SUCCESS_COLOR] textColor:[UIColor whiteColor]];
            vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"Login"];
            // (vc.view.window!.rootViewController?).dismissViewControllerAnimated(false, completion: nil);
            break;


        default:
            break;
    }

    [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc
                                                             withSlideOutAnimation:self.slideOutAnimationEnabled
                                                                     andCompletion:nil];
}

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

    if (indexPath.row == 9) {
        return 0;
    } else {
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }

}

-(void)wipeDataInLogout{

    [self sendDeviceToken];
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"faveoData.plist"];
    NSError *error;
    if(![[NSFileManager defaultManager] removeItemAtPath:plistPath error:&error])
    {
        NSLog(@"Error while removing the plist %@", error.localizedDescription);
        //TODO: Handle/Log error
    }

    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *each in cookieStorage.cookies) {
        [cookieStorage deleteCookie:each];
    }

}

-(void)sendDeviceToken{

   // NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    NSString *url=[NSString stringWithFormat:@"%@fcmtoken?user_id=%@&fcm_token=%s&os=%@",[userDefaults objectForKey:@"companyURL"],[userDefaults objectForKey:@"user_id"],"0",@"ios"];
    MyWebservices *webservices=[MyWebservices sharedInstance];
    [webservices httpResponsePOST:url parameter:@"" callbackHandler:^(NSError *error,id json,NSString* msg){
        if (error || [msg containsString:@"Error"]) {
            if (msg) {

                // [utils showAlertWithMessage:[NSString stringWithFormat:@"Error-%@",msg] sendViewController:self];
                NSLog(@"Thread-postAPNS-toserver-error == %@",error.localizedDescription);
            }else if(error)  {
                //                [utils showAlertWithMessage:[NSString stringWithFormat:@"Error-%@",error.localizedDescription] sendViewController:self];
                NSLog(@"Thread-postAPNS-toserver-error == %@",error.localizedDescription);
            }
            return ;
        }
        if (json) {

            NSLog(@"Thread-sendAPNS-token-json-%@",json);
        }

    }];
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // rows in section 0 should not be selectable
    // if ( indexPath.section == 0 ) return nil;

    // first 3 rows in any section should not be selectable
    if ( (indexPath.row ==0) || (indexPath.row==2) ) return nil;

    // By default, allow row to be selected
    return indexPath;
}


@end

2 个答案:

答案 0 :(得分:1)

做一件事,在viewDidLoad方法中添加类似

的方法
[self update];

然后,在此方法中添加该代码。

-(void)update{


    userDefaults=[NSUserDefaults standardUserDefaults];
    globalVariables=[GlobalVariables sharedInstance];
    NSLog(@"Role : %@",[userDefaults objectForKey:@"role"]);
    _user_role.text=[[userDefaults objectForKey:@"role"] uppercaseString];

    _user_nameLabel.text=[userDefaults objectForKey:@"profile_name"];
    _url_label.text=[userDefaults objectForKey:@"baseURL"];

    [_user_profileImage sd_setImageWithURL:[NSURL URLWithString:[userDefaults objectForKey:@"profile_pic"]]
                          placeholderImage:[UIImage imageNamed:@"default_pic.png"]];
    _user_profileImage.layer.borderColor=[[UIColor hx_colorWithHexRGBAString:@"#0288D1"] CGColor];

    _user_profileImage.layer.cornerRadius = _user_profileImage.frame.size.height /2;
    _user_profileImage.layer.masksToBounds = YES;
    _user_profileImage.layer.borderWidth = 0;


    _view1.alpha=0.5;
    _view1.layer.cornerRadius = 20;
    _view1.backgroundColor = [UIColor purpleColor];

    _view2.alpha=0.5;
    _view2.layer.cornerRadius = 20;
    _view2.backgroundColor = [UIColor purpleColor];


    _view3.alpha=0.5;
    _view3.layer.cornerRadius = 20;
    _view3.backgroundColor = [UIColor purpleColor];


    _view4.alpha=0.5;
    _view4.layer.cornerRadius = 20;
    _view4.backgroundColor = [UIColor purpleColor];

    _view5.alpha=0.5;
    _view5.layer.cornerRadius = 20;
    _view5.backgroundColor = [UIColor purpleColor];


    NSInteger open =  [globalVariables.OpenCount integerValue];
    NSInteger closed = [globalVariables.ClosedCount integerValue];
    NSInteger trash = [globalVariables.DeletedCount integerValue];
    NSInteger unasigned = [globalVariables.UnassignedCount integerValue];
    NSInteger my_tickets = [globalVariables.MyticketsCount integerValue];

    if(open>99){
        _c1.text=@"99+";
    }else
        _c1.text=@(open).stringValue;
    if(closed>99){
        _c4.text=@"99+";
    }else
        _c4.text=@(closed).stringValue;
    if(trash>99){
        _c5.text=@"99+";
    }else
        _c5.text=@(trash).stringValue;
    if(unasigned>99){
        _c3.text=@"99+";
    }else
        _c3.text=@(unasigned).stringValue;
    if(my_tickets>99){
        _c2.text=@"99+";
    }else
        _c2.text=@(my_tickets).stringValue;

    [self.tableView reloadData];

}

并添加此代码,

-(void)viewWillAppear:(BOOL)animated{

 [self.tableView reloadData];
    //[self.tableView reloadData];

}

并在viewDidLoad方法中调用getDependencies方法。 检查一下,让我知道..!

答案 1 :(得分:0)

您可以为需要更新的所有标签创建IBOutlet。您可以在控制器类的任何位置更新计数,而不是在更新计数时重新加载tableview。您可以在单独的方法中移动与更新计数相关的所有代码,并在需要时调用此方法。

 func updateAllLabels() {
    NSInteger open =  [globalVariables.OpenCount integerValue];
    NSInteger closed = [globalVariables.ClosedCount integerValue];
    NSInteger trash = [globalVariables.DeletedCount integerValue];
    NSInteger unasigned = [globalVariables.UnassignedCount integerValue];
    NSInteger my_tickets = [globalVariables.MyticketsCount integerValue];

    if(open>999){
        _inbox_countLabel.text=@"999+";
    }else
        _inbox_countLabel.text=@(open).stringValue;
    if(closed>999){
        _closed_countLabel.text=@"999+";
    }else
        _closed_countLabel.text=@(closed).stringValue;
    if(trash>999){
        _trash_countLabel.text=@"999+";
    }else
        _trash_countLabel.text=@(trash).stringValue;
    if(unasigned>999){
        _unassigned_countLabel.text=@"999+";
    }else
        _unassigned_countLabel.text=@(unasigned).stringValue;
    if(my_tickets>999){
        _myTickets_countLabel.text=@"999+";
    }else
        _myTickets_countLabel.text=@(my_tickets).stringValue;
}

在值更新时调用此updateAllLabels方法。如果您知道只有一个值会更新,则只能更新该标签。