我知道这个问题一再被问到。 This post总结了一些常见原因,但没有一个适用于我:
我在搜索时看到的每个答案都有以下变化: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的调用,一个返回0,这是正常的,因为竞争对手信息尚未加载,另一个返回大于0的数字。但是,没有一个调用cellForRowAtIndexPath例。
答案 0 :(得分:4)
经过几个小时的调试,我终于找到了问题:我没有设置支柱和弹簧,使得表被挤压到0高度。因为未显示,所以未调用cellForRowAtIndexPath