【发布时间】: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