【问题标题】:Update UI table View from a Static method从静态方法更新 UI 表视图
【发布时间】:2017-01-03 06:45:40
【问题描述】:

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

在应用委托中收到新消息时。它应该将它发送到我的incomingChat 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。

我遇到了这个错误

/Users/zupportdesk/Desktop/MyIOSApps/Chat System/Chat System/InCommingVC.m:96:23:选择器没有已知的类方法 'incommingTable'

在做的时候

[[InCommingVC incommingTable] reloadData];

【问题讨论】:

    标签: ios objective-c uitableview static-methods


    【解决方案1】:

    两种方式:

    1 - 创建一个共享实例。致电:

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

    2 - 让您上课确认某些通知,您将在收到带有有效负载(聊天词典)的消息时发送。确保在视图控制器取消分配时取消注册通知

    【讨论】:

      【解决方案2】:
       [self.incommingTable reloadData];
      

      请试试这个并确保 IBOUTLET 连接正确

      【讨论】:

      • hy tnx。是的,它连接正确。它无法检测到自我,因为它是一个静态方法。
      • 你能用 NSNotification 代替静态方法吗。我不确定你现在的情况。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      相关资源
      最近更新 更多