2012-11-04 32 views
0

泰伯維顯示而空, 無法理解我怎麼忘了:泰伯維創建程序是空

#import <UIKit/UIKit.h> 

@interface MenuViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> 
@property (strong, nonatomic) UITableView *table; 
@end 


#import "MenuViewController.h" 

@interface MenuViewController() 

@end 

@implementation MenuViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _table.delegate = self; 
    _table.dataSource = self; 
    _table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; 
    [self.view addSubview:_table]; 
    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 
- (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    NSLog(@"%s",__PRETTY_FUNCTION__); 
    [_table reloadData]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return 4; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 

    cell.textLabel.text = @"text"; 

    // Configure the cell... 

    return cell; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 25; 
} 
+0

設置委託和數據源後** **創建表格。 –

回答

0

viewDidLoad你實例之前分配_table變量的dataSourcedelegate。所以當你初始化_table變量時,它將覆蓋已經存在的任何東西。

如果您的委託和數據源的分配之前移動初始化調用它應該正常工作:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; 
    _table.delegate = self; 
    _table.dataSource = self; 
    [self.view addSubview:_table]; 
} 
0

只是註釋此行:

[self.view addSubview:_table];