2012-01-04 118 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