我正在创建一个iPhone应用程序,我在TableView
和添加按钮时遇到了一些问题。我从常规视图开始,然后添加了一个表格视图,然后在顶部添加了几个按钮,如下所示:
问题在于,当我运行应用程序时,我得到了这个:
我添加的按钮都没有。这是我的.h文件:
//
// RootBeerTVCViewController.h
// BaseApp
//
// Created by Blaine Anderson on 10/12/12.
// Copyright (c) 2012 UIEvolution, Inc. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RootBeerTVCViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *nameButton;
@property (weak, nonatomic) IBOutlet UIButton *locationButton;
@property(strong, nonatomic) NSMutableArray* rootList;
-(IBAction)sort:(id)sender;
@end
这是.M文件:
//
// RootBeerTVCViewController.m
// BaseApp
//
// Created by Blaine Anderson on 10/12/12.
// Copyright (c) 2012 UIEvolution, Inc. All rights reserved.
//
#import "RootBeerTVCViewController.h"
#import "GlobalData.h"
@interface RootBeerTVCViewController ()
@end
@implementation RootBeerTVCViewController
@synthesize rootList;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[GlobalData sharedData].mViewManager.mNavController.navigationBarHidden=NO;
// Do any additional setup after loading the view from its nib.
rootList = [[GlobalData sharedData] mRootList ];
NSLog(@"RootList in view did load: %@", rootList);
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
//[tableView addSubview:sortingView];
[tableView reloadData];
self.view = tableView;
}
- (void)viewDidUnload
{
[self setLocationButton:nil];
[self setNameButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
rootList = [[GlobalData sharedData] mRootList ];
NSLog(@"RootList: %@", rootList);
// Return the number of sections.
NSLog(@"RootList count: %i", rootList.count);
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of sections.
NSLog(@"RootList row count: %i", rootList.count);
return rootList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
rootList =[[GlobalData sharedData].mRootBeerParser rootBeerList];
NSLog(@"RootList Cell: %@", rootList);
RootBeers* mRootBeer = [rootList objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSLog(@"Cell Name: %@", mRootBeer.brewer);
// Configure the cell...
cell.textLabel.text = mRootBeer.name;
cell.detailTextLabel.text = mRootBeer.location;
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RootBeers* mRootBeer =[rootList objectAtIndex:indexPath.row];
[GlobalData sharedData].mRootBeer = mRootBeer;
[[GlobalData sharedData].mViewManager pushView:DETAILVIEW animated:YES];
}
- (IBAction)sort:(id)sender {
rootList =[[GlobalData sharedData].mRootBeerParser rootBeerList];
[[GlobalData sharedData].mRootBeerParser sortRootBeerByName:rootList];
}
@end
如果有人能让我知道我做错了什么,那就太好了。我希望我提供了足够的信息,如果没有,请告诉我,我会很乐意提供更多信息。
答案 0 :(得分:3)
在viewDidLoad
中,您创建一个新的UITableView
屏幕大小,然后将其设置为VC视图,从而覆盖从xib加载的视图。因此,您只能看到您在代码中创建的表。