我在UITableViewreloadData方法上遇到严重问题。我有一个UIViewController类,其中有一个WiGiMainViewControllerUITableViewNSMuttableArray。我目前正在AppDelegate中发出网络呼叫,并在数据下载后将通知发布到WiGiMainViewController。在通知的选择器方法reloadWigiList中,我传递了一个包含最近下载项目的NSArray。然后,使用传入的数组初始化WiGiMainViewControllerNSMuttableArray,并继续在reloadData()对象上调用UITableView。从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;

 }

最佳答案

哇!经过三天的脑力激荡,这个问题变得非常简单。在WiGiMainViewController的ViewDidLoad方法中,我正在初始化表格视图:

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


因为我已经将在IB中创建的tableView链接到实例wigiLists,所以在ViewDidLoad中分配和初始化覆盖了我在IB中创建并当前显示的tableView的链接。摆脱这条线可以解决所有问题。

关于ios - UITableView不重新加载数据或调用cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5363350/

10-14 23:13