2013-01-10 41 views
0

我知道这个问题已被问及一次又一次。 This post总结了一些常见的原因,但没有适用于我:另一个“reloadData调用numberOfRows,但不cellForRowAtIndexPath”

每一个答案,当搜索一直是一个变化我看到: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被称为在任何情况下都没有。

+0

这可能是一个愚蠢的评论,但是在你的代码中'competions.count'(你记录的)返回的结果不同于[self.product.competitors [ (self.conditionValue)] count];'(你回来了吗? –

+0

刚刚尝试返回'competitor.count','countForRowAtIndexPath'不被调用,当count是5时。 –

回答

4

经过几个小时的调试,我终于发现问题所在:我没有将支柱和弹簧正确设置,使桌子被挤压到0高度。因为它没有被显示,所以cellForRowAtIndexPath没有被调用

相关问题