2012-01-04 125 views
1

我已经用google搜索并在这里搜索了stackoverflow,但一直没有找到解决办法。为什么我的UITableView显示不能?

我期待发生的事情:的UITableView显示和显示单元包含 “喜”

会发生什么:的UITableView甚至不显示

viewViewController.h

#import <UIKit/UIKit.h> 

@interface viewViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UITableView *tableView; 
//note: the above property is connected to my UITableViewController 

@end 

viewViewController.m

#import "viewViewController.h" 

@interface viewViewController() 

@end 

@implementation viewViewController 
@synthesize tableView = _tableView; 

- (UITableView *)tableView 
{ 
    if (!_tableView){ 

     _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; 
    } 
    return _tableView; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    //_tableView = [[UITableView alloc] init]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    //[self setTableView:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    //warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"asking for rows"); 
    //#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    switch (section) 
    { 
     case 0: 
      return 5; 
     case 1: 
      return 10; 
      break; 
     default: 
      return 0; 
      break; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    cell.textLabel.text = @"hi"; 
    return cell; 

} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return @"testtext"; 
} 


@end 

请告诉我我做错了什么,并更正了我的代码。

回答

5
  1. 添加一些协议,以您的视图控制器:

    @interface viewViewController:UIViewController中

  2. 检查,以确保你从tableView连接delegatedatasource属性文件的所有者。

+0

FileOwner是什么意思?对不起,我有点新东西。 – blake305 2012-01-04 21:43:52

+0

另外,我使用故事板,如果这有助于任何。 – blake305 2012-01-04 21:44:22

+0

@ blake305他的意思是任何人要实现协议方法,即委托,在你的情况下,它是viewViewController。如果您无法使用Storyboard在代理中连线,请在viewViewController的viewDidLoad中执行'_tableView.delegate = self'和'_tableView.dataSource = self' - 假设您将协议包含在标题中:'@interface viewViewController:UIViewController ' – Jeremy 2012-01-04 22:03:38

0

尝试继承UITableViewController而不是UIViewController。

+0

解释?我没有UITableViewController。我在UIViewController中有一个UITableView,因为它有益于我正在做的事情。 – blake305 2012-01-04 21:22:12

+0

对不起。如果你需要使用UIViewController而不是UITableViewController,那么我认为Tomasz的答案是正确的。 Tomasz说,你需要成为UITableViewDelegate和UITableViewDataSource的代表。 – dean 2012-01-04 21:33:45

1

目前还不清楚UITableView与您的实例变量连接的含义。如果你的意思是从XIB连接起来,那么你可能会从你创建的重载getter方法中获得“干扰”。你可能想要放一个NSlog来查看该方法是否被调用。如果是这种情况,您分配/邀请的新表视图的框架大小为零,并且我没有看到代码中的任何位置可以更改框架大小。

祝你好运!

+0

我在' - (UITableView *)tableView {'之后做了一个NSLog,它没有被调用。我该怎么办? – blake305 2012-01-04 21:24:20

+0

然后你会想遵循tomasz-wojtkowiak的建议,并确保你实现了各种UITableView协议和数据源方法。 – timthetoolman 2012-01-04 21:28:54