UITableView
的reloadData
方法存在严重问题。我有一个 UIViewController
类,WiGiMainViewController
,里面有一个 UITableView
和 NSMuttableArray
。我目前在 AppDelegate
中发出网络调用,并在下载数据后向 WiGiMainViewController
发送通知。在我的通知选择器方法 reloadWigiList 中,我传递了一个包含最近下载的项目的 NSArray
。然后我用传入的数组初始化 WiGiMainViewController
的 NSMuttableArray
并继续在我的 UITableView
上调用 reloadData()
> 对象。我可以从 NSLog
语句中看到 numberOfRowsInSection
在重新加载时触发,但不是 cellForRowAtIndexPath
,因此导致 UI 不重新加载 带有新下载项目的 UITableView
。我已验证 reloadData
方法正在主线程上调用,数据源委托(delegate)设置在 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;
}
最佳答案
哇!经过 3 天的努力解决这个问题后,它变得简单得可笑。在 WiGiMainViewController 的 ViewDidLoad 方法中,我正在初始化我的 tableview:
self.wigiLists = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
因为我已经将我在 IB 中创建的 tableView 链接到我的实例 wigiLists,ViewDidLoad 中的分配和初始化覆盖了我到 IB 中创建的 tableView 的链接,并且当前正在显示。去掉这条线就可以解决所有问题。
关于ios - UITableView 不重新加载数据或调用 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5363350/