TableView是否未加载数据? [英] TableView is not loading data?

查看:57
本文介绍了TableView是否未加载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Map应用程序中,我在主屏幕上具有细分控制器,并使用它具有Map View&tableview(都是UIView).我已经尝试了所有方法,但是我的数据没有加载到表视图中.这是我的tableview代码.这里标记是我的xml文件中的属性,其中包含Showroom名称,并且Iam能够解析此名称..h文件

In my Map application I have segment controller on main screen and using that I have Map View & tableview(both are UIView). I have tried everything but my data is not loading in my tableview. Here is my tableview code. Here marker is attribute in my xml file which contain Showroom name and Iam able to parse this. .h file

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

.m文件

已编辑 =使用ViewWillAppear,viewDieLoad,细分操作方法

Edited = with ViewWillAppear,viewDieLoad, segement action method

 @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上显示它.但是我无法在表视图中加载数据.

In my tableview Header it shows data count correctly but not showing data. I have connected delegate,datasource,_tableview to fileOwner of the HViewController in which I have put above code. Plz suggest something where I am wrong. I am parsing XML file and getting data in console alos I can show it on Map. But I am not able to load data in my tableview.

推荐答案

尝试将 [_ tableView reloadData] 移至 viewWillAppear .

UITableViewController reloads the table view's data in viewWillAppear, not viewDidLoad. I can't tell you the exact reason for which this would make a difference, though I can think of several. Anyway, it's worth a try.


对评论的回应

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

  1. If titleForHeaderInSection: is being called, then there is a data source connected to a table view. So, the problem is not a lack of a data source connection.

我想您的.xib文件中有2个表格视图:一个很大的&在它下面的一小段.大表视图未连接到数据源,因此仅显示空白行.短表视图 已连接到数据源.但是,它仅足够用于页眉,没有剩余空间来显示任何单元格.因此,调用了 titleForHeaderInSection:,但是 cellForRowAtIndexPath:并不是因为没有空间显示任何单元格.
请注意,这只是一个猜测,但这是我能想到的唯一会导致您描述的行为的情况.您发布的代码看起来还可以,尽管比所需的要复杂一些.

I am guessing you have 2 table views in your .xib file: a large one & a short one below it. The large table view is not connected to the data source, so it just displays blank lines. The short table view is connected to the data source. But, it is just tall enough for a header and has no space left to display any cells. Thus, titleForHeaderInSection: is called, but cellForRowAtIndexPath: is not because there is no space to display any cells.
Note that this is just a guess, but it is the only scenario I can think of that would cause the behavior you described. The code you posted looks ok, although a bit more complicated than necessary.

毫无疑问, reloadData 应该位于 viewWillAppear 中.Apple工程师在创建UITableViewController类时就将其放置在那里.因此,要将其放到其他地方,您必须相信自己比他们更了解.

There is no question that reloadData should be in viewWillAppear. That's where the Apple engineers put it when they created the UITableViewController class. So, to put it elsewhere, you have to believe you know better than they do.

这篇关于TableView是否未加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆