[英]UITableView reloadData doesn't work
我已经阅读了所有类似的问题,并尝试了所有建议,但还是没有。 也许有人可以发现我的缺点。
我的视图控制器由另一个视图控制器通过两个按钮之一启动。 点击按钮会发送NSNotification(带有附加的数组),并且此视图控制器会预期此通知,然后调用此方法:
- (void)addContentToArray:(NSNotification *)aNotification {
array = [NSArray arrayWithArray:[aNotification object]];
([array count] == 6) ? (category = YES) : (category = NO);
[myTableView reloadData];
NSLog(@"%d", [array count]);
NSLog(@"%@", myTableView);
}
每次都会调用该方法,我可以从更改数组计数中看到这一点。 此处的通知对象是从以前的视图控制器传递的数组,我将这些对象分配给本地数组属性-这是我的UITableView源。 因此,我要做的是尝试重用UITableView来显示要传递的任何数组的元素。 它对于传递的第一个数组(以先到者为准)效果很好。
当我点击第二个按钮时,新数组成功传递(如前所述,我从[array count]的日志中知道这是不同的:不同数组中3个对象与6个对象)。 但是,没有发生的事情是UITableView不会刷新(尽管我选择表中的行时传递的值来自正确的数组,即使显示了错误的值)。
以下是UITableView数据源方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Identifier"];
}
cell.textLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:@"name"];
if (category) {
cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:@"description"];
}
return cell;
}
那么,我在做什么错呢?
其他一些考虑因素可能会有所帮助:
我将不胜感激和帮助!
需要更多代码,该代码是您在其中创建的,并调用Myviewcontroller
的addContentToArray
方法。 我认为您在其中使用了Myviewcontroller
对象的发布代码,请尝试隐藏该部分。
我设法通过编辑initWithNibName方法解决了该问题(旧行已注释掉)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self = [super initWithNibName:@"PurposeCategoryViewController" bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
显然,它确实与我的视图控制器(称为PurposeCategoryViewController)不是PKRevealController层次结构中的顶部/焦点视图控制器有关。 因此,我只需要具体指出我的笔尖文件即可。
您在此委托方法中返回的值是:-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView确保它为1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.