我知道这个问题已经被问了一遍又一遍。这篇文章总结了一些常见的原因,但没有一个适用于我:
我在搜索时看到的每个答案都有以下变化:1)tableView 为零 2)numberOfRowsInSection 为 0 3)tableView 的委托/数据源未设置 4)在错误的 uiTableView 上调用 reloadTable。
该帖子的答案是在再次调用 reloadData 之前未显示 tableView,这也不是我的情况。我的实际代码有点长,所以我只粘贴我认为相关的部分。随时要求我粘贴更多。请注意,competitorsTable
已添加到故事板的视图中。
@interface CartItemViewController : TrackedUIViewController <UITableViewDataSource, UITableViewDelegate>
//...
@end
@interface CartItemViewController ()
//...
@property (weak, nonatomic) IBOutlet UITableView *competitorsTable;
@end
@implementation CartItemViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
NSAssert(self.competitorsTable, @"Competitor table should not be nil");
self.competitorsTable.dataSource = self;
self.competitorsTable.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self updateCompetitors];
}
- (void)updateCompetitors
{
MBProgressHUD *indicator = [MBProgressHUD showHUDAddedTo:self.hostView animated:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (![self.product isLoaded]) {
[NSThread sleepForTimeInterval:1];
}
[self.product loadCompetitorsPriceForConditionValue:self.conditionValue];
NSDictionary *competitors = self.product.competitors[@(self.conditionValue)];
dispatch_async(dispatch_get_main_queue(), ^{
if (competitors) {
if (competitors.count > 1) {
self.hostView.hidden = NO;
self.hostView.hostedGraph = [[CompetitorGraph alloc]initWithFrame:self.hostView.bounds Competitors:competitors];
NSAssert(!self.competitorsTable.hidden, @"Competitor table should not be hidden");
[self.competitorsTable reloadData];
} else {
self.hostView.hidden = YES;
}
} else {
self.hostView.hostedGraph = nil;
}
[indicator hide:YES];
});
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSDictionary *competitors = self.product.competitors[@(self.conditionValue)];
if (competitors.count == 0) {
NSLog(@"WARNING: %s returning 0", __PRETTY_FUNCTION__);
} else {
NSLog(@"Number of rows: %d", competitors.count);
}
return [self.product.competitors[@(self.conditionValue)] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Sort names according to its price
NSDictionary *competitors = self.product.competitors[@(self.conditionValue)];
NSArray *names = [competitors.allKeys sortedArrayUsingComparator:^NSComparisonResult(id name1, id name2) {
if ([competitors[name1] floatValue] > [competitors[name2] floatValue]) {
return (NSComparisonResult)NSOrderedAscending;
} else if ([competitors[name1] floatValue] < [competitors[name2] floatValue]) {
return (NSComparisonResult)NSOrderedDescending;
} else {
return (NSComparisonResult)NSOrderedSame;
}
}];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"vendor"];
NSString *vendorName = names[indexPath.row];
cell.textLabel.text = vendorName;
cell.detailTextLabel.text = competitors[vendorName];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"Competitors' Offer";
}
@end
打开视图时,有两次调用numberOfRowsInSection,一次返回0,这是正常的,因为没有加载竞争对手的信息,一次返回一个大于0的数字。但是在任何情况下都没有调用cellForRowAtIndexPath。