2011-03-17 51 views
1

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

@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; 

} 
+0

你可以发布在tableView上调用reloadData的代码吗?还有你如何检查是否正在调用cellForRowAtIndexPath:? – MCannon 2011-03-17 20:43:55

+0

发布代码...我把日志语句放在cellForRowAtIndexPath中让我知道它是否以及何时被调用。我可以从日志语句中看到numberOfRowsInSection正在使用更新的itemsList计数进行调用。 – 2011-03-17 20:54:46

+0

我更新了代码,现在使用通知让UIViewController知道何时重载数据,但仍然是相同的问题,虽然 – 2011-03-18 19:52:50

回答

0

哇!经过3天的反对这个问题,我觉得这简直太荒唐了。在WiGiMainViewController我viewDidLoad方法,我是初始化我的tableview:

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

因为我已经挂了的tableView我在IB创建我的实例wigiLists,alloc'ing并在viewDidLoad中初始化改写我的链接的tableView创建在IB中并且当前正在显示。摆脱这条线固定了一切。

0

第一件事就是设置一个断点[self.wigiMainViewController.wigiLists reloadData]

如果wigiLists的tableView显示在调试器为null,则它可能尚未插座组。另外,确保委托和数据源都已设置。

+0

我知道委托和数据源不为空,因为我在尝试reloadData之前将它们打印在日志中。我会放置断点并尝试通过这个工作。谢谢 – 2011-03-17 21:11:54

+0

我已验证tableview,在我的情况下wigiItems在调用reloadData时不为null。 – 2011-03-18 03:12:52

+0

如果我更改numberOfRowsInSection返回1如果itemsList计数为0,那么我可以看到当我滚动时在tableview中的第一个项目。不知道这是否相关。 – 2011-03-18 14:46:28

相关问题