我遇到了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;
}
你可以發佈在tableView上調用reloadData的代碼嗎?還有你如何檢查是否正在調用cellForRowAtIndexPath:? – MCannon 2011-03-17 20:43:55
發佈代碼...我把日誌語句放在cellForRowAtIndexPath中讓我知道它是否以及何時被調用。我可以從日誌語句中看到numberOfRowsInSection正在使用更新的itemsList計數進行調用。 – 2011-03-17 20:54:46
我更新了代碼,現在使用通知讓UIViewController知道何時重載數據,但仍然是相同的問題,雖然 – 2011-03-18 19:52:50