2011-03-17 50 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

相關問題