无法获取UITableView来显示NSArray

时间:2011-09-21 17:18:00

标签: iphone objective-c

听起来像是一个已经回答的问题...也许但是我已经检查了所有可能的解决方案,我相信并且没有任何对我有用。

我的.h文件看起来像这样

#import <UIKit/UIKit.h>

@interface epsMenuPage : UIViewController<UITableViewDelegate,UITableViewDataSource>{
NSArray *listData;
...
UITableView *menuList;
...
}
@property (nonatomic,retain) NSArray *listData;
...
@property (nonatomic, retain) IBOutlet UITableView *menuList;
...
@end

我的.m文件看起来像这样

@implementation epsMenuPage
@synthesize listData;
...
@synthesize menuList;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidLoad
{ 
//Arrays credit,debit and both
creditListData = [[NSArray alloc]initWithObjects:@"Return",@"Force Sale",@"Authorize    Only",@"Total Sales",@"Items Sold",@"Reciept's", nil];
debitListData = [[NSArray alloc]initWithObjects:@"Return",@"Force Sale",@"Authorize Only",@"Total Sales",@"Items Sold",@"Reciept's", nil];
bothListData = [[NSArray alloc]initWithObjects:@"Batch History",@"Settle Batch", nil];
self.listData = creditListData;
[menuList reloadData];

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.view.backgroundColor =[UIColor colorWithRed:255/255.0 green:95/255.0 blue:95/255.0 alpha:1.0];
[colorChange setBackgroundColor:[UIColor colorWithRed:255/255.0 green:95/255.0 blue:95/255.0 alpha:1.0]];
[self.view addSubview:creditTabButton];
[self.view addSubview:debitTabButton];
[self.view addSubview:bothTabButton];
[self.view addSubview:creditButtonText];
[self.view addSubview:debitButtonText];
[self.view addSubview:bothBottonText];
[self.view addSubview:colorChange];

}

- (void)viewDidUnload
{
[self setTitleBAckButton:nil];
[self setTitleSettingsButton:nil];
[self setCreditTabButton:nil];
[self setDebitTabButton:nil];
[self setBothTabButton:nil];
[self setMenuList:nil];
[self setColorChange:nil];
[self setCreditButtonText:nil];
[self setDebitButtonText:nil];
[self setBothBottonText:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *SimpleTableIdentifier = @"tableID";
    UITableViewCell *cell = [tableView
                                dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:SimpleTableIdentifier] autorelease];
        }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.listData objectAtIndex:row];
    return cell;

    }

- (void)dealloc {
[titleBAckButton release];
[titleSettingsButton release];
[creditTabButton release];
[debitTabButton release];
[bothTabButton release];
[menuList release];
[colorChange release];
[creditButtonText release];
[debitButtonText release];
[bothBottonText release];
[listData release];
[super dealloc];
}
 - (IBAction)titleBackClick:(id)sender {
[self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (IBAction)titleSettingsClick:(id)sender {
}

- (IBAction)creditTabClick:(id)sender {
self.listData = creditListData;
[menuList reloadData];
[colorChange setBackgroundColor:[UIColor colorWithRed:255/255.0 green:95/255.0 blue:95/255.0 alpha:1.0]];
[self.view addSubview:creditTabButton];
[self.view addSubview:creditButtonText];
[self.view addSubview:colorChange];
self.view.backgroundColor = [UIColor colorWithRed:255/255.0 green:95/255.0 blue:95/255.0 alpha:1.0];
}

- (IBAction)debitTabClick:(id)sender {
self.listData = debitListData;
[menuList reloadData];
[colorChange setBackgroundColor:[UIColor colorWithRed:244/255.0 green:133/255.0 blue:33/255.0 alpha:1.0]];
[self.view addSubview:debitTabButton];
[self.view addSubview:debitButtonText];
[self.view addSubview:colorChange];
self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:133/255.0 blue:33/255.0 alpha:1.0];
}

- (IBAction)bothTabClick:(id)sender {
self.listData = bothListData;
[menuList reloadData];
[colorChange setBackgroundColor:[UIColor colorWithRed:72/255.0 green:72/255.0 blue:255/255.0 alpha:1.0]];
[self.view addSubview:bothTabButton];
[self.view addSubview:bothBottonText];
[self.view addSubview:colorChange];
self.view.backgroundColor = [UIColor colorWithRed:72/255.0 green:72/255.0 blue:255/255.0 alpha:1.0];
}
@end

我真的为我的生活无法弄清楚为什么它不会显示在我的TableView上,我已经尝试了很多不同的教程。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您永远不会将menuList表格视图添加到您的视图中。添加它,看看是否有诀窍:

// In -viewDidLoad

[self.view addSubview:menuList];

编辑1 :您是否在.h中声明了属性?这些不可见,因此看起来好像您没有创建或将表视图添加到视图控制器的视图中。但是,如果您使用IBOutlet,那么这可能不是问题。

编辑2 :根据以下评论,您的dataSource为零,这正是为什么没有显示的原因。确保Interface Builder中的表视图的dataSource出口指向您的视图控制器。为了确保双重,请从Interface Builder中删除表视图并再次添加。确保您的连接完全正确。还可以尝试清理项目并重建,以及重新启动Xcode。

答案 1 :(得分:-1)

看起来您缺少必需的数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
  return 1;
}

此外,看起来您实际上并未初始化UIButton,或者您要添加的任何子视图。你在使用笔尖吗?