gpt4 book ai didi

ios - 从 appdelegate 调用 UITableView 的 reloadData 方法

转载 作者:行者123 更新时间:2023-11-28 17:46:49 28 4
gpt4 key购买 nike

我在使用 UITableView reloadData 方法时遇到严重问题。我有一个 UIViewController 类 WiGiMainViewController,其中有一个 UITableView 和 NSMuttableArray。我目前正在 AppDelegate 中发出网络调用,并在下载数据后将通知发布到 WiGiMainViewController。在我的通知选择器方法 reloadWigiList 中,我传递了一个包含最近下载的项目的 NSArray。然后,我使用传入的数组初始化 WiGiMainViewController 的 NSMuttableArray,然后继续在我的 UITableView 对象上调用 reloadData()。我可以从 nslog 语句中看到,在重新加载时触发了 numberOfRowsInSection 而不是 cellForRowAtIndexPath,因此导致 UI 不使用新下载的项目重新加载 UITableView。我已验证正在主线程上调用 reloadData 方法,并且数据源委托(delegate)在 IB 中设置,并以编程方式在 WiGiMainViewController 的 viewDidLoad 方法中设置。知道为什么我的 UITableView、wigiLists 没有重新加载数据,特别是没有调用 cellForRowAtIndexPath 方法吗?

@interface WiGiMainViewController : UIViewController<FBRequestDelegate,UITableViewDelegate, UITableViewDataSource> {

//setup UI
UITableView *wigiLists;

WiGiAppDelegate *myAppDelegate;
NSMutableArray *itemsList;

}
@property (nonatomic, retain) WiGiAppDelegate *myAppDelegate;
@property (nonatomic, retain) IBOutlet UITableView *wigiLists;
@property (nonatomic, retain) NSMutableArray *itemsList;

-(void) reloadWigiList: (NSNotification*) notification;
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView;
-(NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection:(NSInteger) section;
-(UITableViewCell *) tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath;

@end


@implementation WiGiMainViewController

@synthesize headerLabel = _headerLabel, userNameLabel = _userNameLabel, facebookPicture = _facebookPicture,
myAppDelegate, wigiLists, itemsList;

- (void)viewDidLoad {
NSLog(@"In viewDidLoad");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWigiList:) name:@"wigiItemUpdate" object:nil];


// get appdelicate
self.myAppDelegate = (WiGiAppDelegate*) [[UIApplication sharedApplication] delegate];
self.itemsList = [[NSMutableArray alloc] init];
//setup tableview
self.wigiLists = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.wigiLists.delegate = self;
self.wigiLists.dataSource = self;
//set up view
[self.headerLabel setText:self.myAppDelegate.HEADER_TEXT];
//check if user is logged in
if (self.myAppDelegate.isLoggedIn) {
//user is logged in
NSLog(@"HERE");
//get facebook information to populate view
[self retrieveFacebookInfoForUser];
//[self.myAppDelegate retrieveWigiItems];
}else {
//user is not logged in
NSLog(@"user not logged in");
//show login modal
}
//[self.wigiLists reloadData];
[super viewDidLoad];
}

-(void) reloadWigiList: (NSNotification *) notification {
if ([NSThread isMainThread]) {
NSLog(@"main thread");
}else{
NSLog(@"METHOD NOT CALLED ON MAIN THREAD!");
}
NSLog(@"notification recieved:%@", notification.userInfo);

NSLog(@"in reloadwigilists:%@", wigiLists );
NSLog(@"list size:%@", self.itemsList);
NSLog(@"delegate:%@",self.wigiLists.delegate);
NSLog(@"datasource:%@",self.wigiLists.dataSource);
//populate previously empty itemsList
[self.itemsList setArray:notification.userInfo];
NSLog(@"list size:%@", self.itemsList);
[self.wigiLists reloadData];

}

/////////////////////////////////////
// UITableViewDelegate protocols
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
//NSLog(@"numberofsections: %@", [self.itemsList count]);
return 1;
}

-(NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
NSLog(@"7t87giuiu%@",self.itemsList);
NSLog(@"numberofrows: %i", [self.itemsList count]);


return [self.itemsList count];

//return 6;

}

最佳答案

我要做的第一件事是在 [self.wigiMainViewController.wigiLists reloadData] 上设置一个断点

如果 wigiLists tableView 在调试器中显示为 null,那么它可能是一个尚未设置的 socket 。另外,确保已设置委托(delegate)和数据源。

关于ios - 从 appdelegate 调用 UITableView 的 reloadData 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5344525/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com