从appdelegate调用UITableView的reloadData方法
我在使用 UITableView reloadData 方法时遇到严重问题。我有一个 UIViewController 类 WiGiMainViewController ,其中有一个 UITableView 和 NSMutableArray 。我目前正在 AppDelegate 中发出网络调用,并在数据下载后向 WiGiMainViewController 发布通知。在我的通知选择器方法 reloadWigiList 中,我传递了一个包含最近下载的项目的 NSArray。然后,我使用传入的数组初始化 WiGiMainViewController 的 NSMutableArray,并继续在我的 UITableView 对象上调用 reloadData()。我可以从 nslog 语句中看到,重新加载时会触发 numberOfRowsInSection,但不会触发 cellForRowAtIndexPath,因此导致 UI 不会使用新下载的项目重新加载 UITableView。我已经验证了 reloadData 方法正在主线程上被调用,并且数据源委托是在 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;
}
I am having a serious issue with UITableView reloadData method. I have a UIViewController class, WiGiMainViewController that has a UITableView and NSMuttableArray in it. I am currently issuing network calls in the AppDelegate, and posting notifications to the WiGiMainViewController once the data has been downloaded. In my selector method for the notification, reloadWigiList, I am passing an NSArray containing the the recently downloaded items. I then initialize the WiGiMainViewController's NSMuttableArray with the passed in array and proceed to call reloadData() on my UITableView object. I can see from nslog statements that the numberOfRowsInSection is fired on reload but not the cellForRowAtIndexPath, therefore causing the UI NOT to reload the UITableView with the newly downloaded items. I have verified that the reloadData method is being called on the main thread and that the datasource delegate are set in IB and programatically in the viewDidLoad method of WiGiMainViewController. Any ideas why my UITableView, wigiLists isn't reloading the data, in particular, not calling the cellForRowAtIndexPath method?
@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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我要做的第一件事是在[self.wigiMainViewController.wigiLists reloadData]上设置一个断点,
如果wigiLists tableView在调试器中显示为空,那么它可能是一个尚未设置的插座。另外,请确保委托和数据源都已设置。
first thing i would do is set a breakpoint on [self.wigiMainViewController.wigiLists reloadData]
if the wigiLists tableView shows a null in the debugger then it is probably an outlet that hasn't been set. also, make sure that both the delegate and datasource have been set.
哇!经过三天的思考,这个问题变得简单得可笑。在 WiGiMainViewController 的 ViewDidLoad 方法中,我正在初始化我的 tableview:
因为我已将在 IB 中创建的 tableView 链接到我的实例 wigiLists,所以在 ViewDidLoad 中分配和初始化覆盖了我到在 IB 中创建且当前正在显示的 tableView 的链接。摆脱这条线就解决了一切。
Wow! after 3 days of banging my head against this problem, it was something ridiculously simple. In my ViewDidLoad method of WiGiMainViewController, i was initializing my tableview:
Because I had linked the tableView I created in IB to my instance wigiLists, alloc'ing and initializing in the ViewDidLoad overwrote my link to the tableView created in IB and currently being displayed. Getting rid of this line fixed everything.