2017-01-03 19 views
0

我正在开发一个聊天应用程序。我的一些类是单例,因此我使用了很多静态方法。更新UI表从静态方法查看

在应用程序委托中收到新消息时。它应该将它发送到我的incomingChat viewController。

我能够在viewcontroller中将新消息传递给静态方法。但我不能从静态方法重新加载表。

InCommingVC.h

#import <UIKit/UIKit.h> 

@interface InCommingVC : UIViewController 
@property (weak, nonatomic) IBOutlet UINavigationBar *navigationBarTitle; 
@property (weak, nonatomic) IBOutlet UITableView *incommingTable; 

+ (void) sendIncommingChats:(NSDictionary *) chatDetails; 
+ (void) recieveIncomingChat:(NSDictionary *) chatDetails; 

@end 

InCommingVC.m

#import "InCommingVC.h" 
#import "AppDelegate.h" 
#import "IncommingItemObject.h" 

static NSMutableArray *incomminglist; 

@interface InCommingVC(){ 
    AppDelegate *delegate; 
} 

@end 

@implementation InCommingVC 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.navigationBarTitle.topItem.title = @"Incomming Chats"; 

} 



+ (void) recieveIncomingChat:(NSDictionary *) chatDetails{ 
    NSLog(@"GOT A NEW recieveIncomingChat"); 
    NSString *CompanyId = [chatDetails objectForKey:@"CompanyId"]; 
    NSString *ConnectionId = [chatDetails objectForKey:@"ConnectionId"]; 
    NSString *CountryCode = [chatDetails objectForKey:@"CountryCode"]; 
    NSString *Department = [chatDetails objectForKey:@"Department"]; 
    NSString *Name = [chatDetails objectForKey:@"Name"]; 
    NSString *StartTime = [chatDetails objectForKey:@"StartTime"]; 
    NSString *TimeZone = [chatDetails objectForKey:@"TimeZone"]; 
    NSString *VisitorID = [chatDetails objectForKey:@"VisitorID"]; 
    NSString *WidgetId = [chatDetails objectForKey:@"WidgetId"]; 

    NSLog(@"------------------------------------------------------------------------------"); 
    NSLog(@"CompanyId  : %@" , CompanyId); 
    NSLog(@"ConnectionId : %@" , ConnectionId); 
    NSLog(@"CountryCode : %@" , CountryCode); 
    NSLog(@"Department  : %@" , Department); 
    NSLog(@"Name   : %@" , Name); 
    NSLog(@"StartTime  : %@" , StartTime); 
    NSLog(@"TimeZone  : %@" , TimeZone); 
    NSLog(@"VisitorID  : %@" , VisitorID); 
    NSLog(@"WidgetId  : %@" , WidgetId); 
    NSLog(@"------------------------------------------------------------------------------"); 

    IncommingItemObject *item = [[IncommingItemObject alloc] init]; 
    [item setCompanyId:CompanyId]; 
    [item setConnectionId:ConnectionId]; 
    [item setCountryCode:CountryCode]; 
    [item setDepartment:Department]; 
    [item setName:Name]; 
    [item setStartTime:StartTime]; 
    [item setTimeZone:TimeZone]; 
    [item setVisitorID:VisitorID]; 
    [item setWidgetId:WidgetId]; 

    if (incomminglist.count == 0) { 
     incomminglist = [[NSMutableArray alloc] init]; 
     [incomminglist addObject:item]; 
     [[InCommingVC incommingTable] reloadData]; 
    } else { 
     [incomminglist addObject:item]; 
    } 

    NSLog(@"count %i", incomminglist.count); 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return incomminglist.count; 
} 

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

    NSString *cellIdentifier = @"identify_incomming"; 
    UITableViewCell *cell = [self.incommingTable dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    IncommingItemObject *item = [incomminglist objectAtIndex:indexPath.row]; 

    UIImageView *CountryImage = (UIImageView *)[cell viewWithTag:5010]; 
    [CountryImage setImage:[UIImage imageNamed:item.CountryCode]]; 

    UILabel *visitorName = (UILabel *)[cell viewWithTag:5011]; 
    visitorName.text = item.Name; 

    UILabel *visitStartTime = (UILabel *)[cell viewWithTag:5012]; 
    visitStartTime.text = item.StartTime; 

    return cell; 
} 

我想我incommingTable从一个静态方法更新。有人能帮我吗。 TNX。

我有此错误

/用户/ zupportdesk /桌面/ MyIOSApps /聊天系统/聊天 系统/ InCommingVC.m:96:23:用于选择 'incommingTable'

没有已知方法类

,而这样做

[[InCommingVC incommingTable] reloadData];

回答

1

2种方式:

1 - 做一个共享实例。呼叫:

[[[self class] sharedInstance].tableView reloadData]; 

2 - 让你确认类的一些通知,你会在收到消息的有效载荷(聊天字典)发送。请务必在视图控制器取消分配

+0

方法2解决了我的问题。 TNX –

0
[self.incommingTable reloadData]; 

试试这个,请,并确保IBOutlet中被正确连接

+0

hy tnx时注销通知。它是正确连接。它不能检测到自己,因为它是一种静态方法。 –

+0

你可以使用NSNotification而不是静态方法吗?我不确定你目前的状况。 –