本文介绍了UITableView 不重新加载数据或调用 cellForRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a serious issue with UITableView's 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;

 }

推荐答案

哇!在我用头解决这个问题 3 天之后,这是一件非常简单的事情.在我的 WiGiMainViewController 的 ViewDidLoad 方法中,我正在初始化我的 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];

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

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.

这篇关于UITableView 不重新加载数据或调用 cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:43