この質問が何度も繰り返されていることは知っています。この投稿では、いくつかの一般的な原因をまとめましたが、私には当てはまりません:
検索時に見たすべての回答は、次のバリエーションでした。1) tableView が nil 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 への呼び出しが 2 回あります。1 つは、競合他社の情報が読み込まれていないため正常な 0 を返し、もう 1 つは 0 より大きい数値を返します。ただし、cellForRowAtIndexPath はいずれも呼び出されません。