从appdelegate调用UITableView的reloadData方法

发布于 2024-10-24 18:43:57 字数 3577 浏览 3 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

破晓 2024-10-31 18:43:57

我要做的第一件事是在[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.

倾城°AllureLove 2024-10-31 18:43:57

哇!经过三天的思考,这个问题变得简单得可笑。在 WiGiMainViewController 的 ViewDidLoad 方法中,我正在初始化我的 tableview:

self.wigiLists = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

因为我已将在 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:

self.wigiLists = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文