【问题标题】:TableView is not loading data?TableView 没有加载数据?
【发布时间】:2011-11-12 06:46:07
【问题描述】:

在我的地图应用程序中,我在主屏幕上有段控制器,使用它我有地图视图和表格视图(都是 UIView)。我已经尝试了一切,但我的数据没有加载到我的表格视图中。这是我的表格视图代码。这里的标记是我的 xml 文件中的属性,其中包含展厅名称,我能够解析它。 .h 文件

@interface HViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>    {      
 UITableView *_tableView;
}
@property (nonatomic, retain) IBOutlet UITableView *_tableView;
@end

.m 文件

已编辑 = 使用 ViewWillAppear、viewDieLoad、分段操作方法

 @synthesize tableView;
 - (void)viewDidLoad {
appDelegate = (HAppDelegate *)[[UIApplication   sharedApplication] delegate];

[segmentedControl addTarget:self action:@selector(segmentAction:)

           forControlEvents:UIControlEventValueChanged];

   }

 - (void)viewWillAppear:(BOOL)animated 
 {
self._tableView.rowHeight = 80.0;

    [_tableView reloadData];
 }

 -(void)segmentAction:(id)sender;{
UISegmentedControl* segCtl = sender ;

if( [segCtl selectedSegmentIndex] == 0 )

{
    NSLog(@"segmentAction mapView");
    mapView.hidden = NO;
    _tableView.hidden = YES;
    //[ self.view addSubview:mapView] ;    // adding view to segmentindex 0

}

if( [segCtl selectedSegmentIndex] == 1 )

{
    NSLog(@"segmentAction _tableview");
    _tableView.hidden = NO;
    mapView.hidden = YES;
    //[ self.view addSubview:_tableview] ;

}
}
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return 1;
   }
  // Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   {
NSLog(@"appDelegate.markers _tableview");

return [appDelegate.markers count];
   }

   //method to print row(showroom count on Header)
   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:  (NSInteger)section {

 if(section == 0)
    return [NSString stringWithFormat:NSLocalizedString(@"ShowRooms[%d]",  @"Showroom format"), [appDelegate.markers count]];
   }

 // Customize the appearance of table view cells.
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {

static NSUInteger const kShowroomNameLabelTag = 2;
UILabel *ShowroomNameLabel = nil;

  static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:CellIdentifier] autorelease];

    cell.selectionStyle = UITableViewCellSelectionStyleGray;   

    ShowroomNameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(50, 1, 300,   20)] autorelease];
    ShowroomNameLabel.tag = kShowroomNameLabelTag;
    ShowroomNameLabel.font = [UIFont boldSystemFontOfSize:18];
    [cell.contentView addSubview:ShowroomNameLabel];
NSLog(@"UITableViewCell.markers _tableview");

}
else 
    {
    ShowroomNameLabel = (UILabel *)[cell.contentView viewWithTag:kShowroomNameLabelTag];


}   

marker *aMarker = [appDelegate.markers objectAtIndex:indexPath.row];
//ShowroomNameLabel.text = aMarker.name;
ShowroomNameLabel.text= [NSString stringWithFormat:@"ShowroomName= %@",   aMarker.name];


return cell;
}

在我的表格视图标题中,它正确显示了数据计数,但没有显示数据。 我已将委托、数据源、_tableview 连接到我在其中放置上述代码的 HViewController 的 fileOwner。请提出一些我错的地方。我正在解析 XML 文件并在控制台中获取数据,我可以在 Map 上显示它。但我无法在我的表格视图中加载数据。

【问题讨论】:

  • 我只是在下面的链接上得到了上述问题的最佳答案:) 必须看到....stackoverflow.com/questions/8239543/…。这是非常小的解决方案,但有很大的不同。感谢 Louis 和 salo.dm 的跟进。 salo.dm 帮助我解决了以下答案中的一些错误。再次感谢您。

标签: iphone uitableview


【解决方案1】:

尝试将[_tableView reloadData] 移动到viewWillAppear。

UITableViewController 将表视图的数据重新加载到viewWillAppear,而不是viewDidLoad。我不能告诉你这会产生影响的确切原因,尽管我能想到几个。总之,值得一试。

编辑:
回复评论

  1. 如果正在调用titleForHeaderInSection:,则表示存在连接到表视图的数据源。因此,问题不在于缺少数据源连接。

  2. 我猜你的 .xib 文件中有 2 个表视图:一个大的,一个短的在它下面。大表视图没有连接到数据源,所以它只显示空白行。短表视图连接到数据源。但是,它的高度足以容纳一个标题,并且没有剩余空间来显示任何单元格。因此,titleForHeaderInSection: 被调用,但 cellForRowAtIndexPath: 没有被调用,因为没有空间来显示任何单元格。
    请注意,这只是一个猜测,但这是我能想到的唯一会导致您描述的行为的场景。您发布的代码看起来不错,虽然比必要的要复杂一些。

  3. 毫无疑问reloadData 应该在viewWillAppear 中。这就是 Apple 工程师在创建 UITableViewController 类时放置它的地方。所以,换一种说法,你必须相信你比他们更了解。

【讨论】:

  • 我已经试过你的解决方案它不起作用。我检查了我的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { } 方法没有被设置断点或 NSLoging 调用。我该如何解决这个问题???
  • 下面的方法在tableview底部显示数据计数 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section { if(section == 0) return [NSString stringWithFormat :NSLocalizedString(@"ShowRooms[%d]", @"Showroom 格式"), [appDelegate.markers count]]; } :(:(:(
  • 是的,你的猜测是正确的......我错误地有两个我以前没有观察到的 tableview。我删除的一个表格视图。这解决了我的一个问题。现在我正在获取上述数据。但没有让数据显示:(
  • 仍然是我的 cellForRowAtIndexPath: 没有通过设置断点被调用...我已经重新连接了所有内容,并按照您的建议删除了一个不必要的 tableview。仅使用空白列表获取顶部的数据计数
  • 问题如下。 Interface Builder 会为您创建一个表格视图。但是,之后,在viewDidLoad 中,您创建了一个新的表视图并将 ivar 设置为新的表视图,删除了与 Interface Builder 表视图的连接。但是,您不要调用addSubview: 来添加新的表格视图。因此,新的表视图根本不显示,而 Interface Builder 表不再连接到 IBOutlet ivar。总之,如果您已经在 Interface Builder 中创建了表格视图,则无需在代码中分配/初始化表格视图。
猜你喜欢
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多