2012-03-29 79 views
1

我有一個應用程序,顯示可以購買的產品列表。對於列表中的視圖控制器看起來像這樣:如何在視圖控制器中獲得對UITableView的引用?

@interface InAppPurchaseListUIItemViewController : 
UIViewController <UITableViewDelegate, UITableViewDataSource> { 
    IBOutlet UITableView *_inAppPurchaseListTable; 
. 
. 
. 
} 
. 
. 
. 
@end 

這是一個顯示清單的代碼:

_inAppPurchaseListUIItemViewController = [[InAppPurchaseListUIItemViewController alloc] 
    initWithItemList: [[InAppPurchaseManager getInstance] getAuthorisedInAppPurchaseProducts] 
    :     self 
    andSelector:  @selector(inAppPurchaseSelected:) 
]; 

_inAppPurchaseListCustomUIView= [[InAppPurchaseListCustomUIView alloc] 
    initWithFrame:CGRectMake(0, 0, inapp_purchase_table_width, inapp_purchase_table_height)]; 

[_inAppPurchaseListCustomUIView addSubview:_inAppPurchaseListUIItemViewController.view]; 
_inAppPurchaseListUIItemViewController.view.frame = _inAppPurchaseListCustomUIView.frame; 

InAppPurchaseListCustomUIView子類UIView和否則是一個空類。

還有一個與視圖控制器同名的.xib。 .xib似乎包含一個表視圖,沒有別的。

該列表顯示正常,但現在我想更改顯示的項目。我有一張支持桌子的NSMutableArray。當我改變陣列時,我需要讓UITableView重新繪製新陣列,但_inAppPurchaseListTable總是零。

  • 如何獲得對UITableView的引用,以便我可以撥打reloadData
  • 無論如何,我可以擺脫.xib?我沒有看到一個額外的文件,除了保存一個表格視圖之外什麼都不做。

連接督察
enter image description here

+0

您是否使用xib文件連接了_inAppPurchaseListTable! – Scar 2012-03-29 21:38:45

+0

在.xib中,使用Connections Inspector,您有哪些網點鏈接到表視圖?特別是在「參考插座」部分?另外,initWithItemList:方法是什麼樣的? – 2012-03-29 21:43:57

+0

@Scar,我該怎麼做? – Jay 2012-03-29 21:53:35

回答

2

我想通了最後。答案在Table View Programming Guide for iOS的第43頁。

我需要它添加到視圖控制器:

- (void)loadView 
{ 
    tableView = [[UITableView alloc] 
     initWithFrame: [[UIScreen mainScreen] applicationFrame] 
     style:   UITableViewStylePlain 
    ]; 

    tableView.autoresizingMask = 
     UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

    tableView.delegate = self; 
    tableView.dataSource = self; 
    [tableView reloadData]; 
    self.view = tableView; 
    [tableView release]; 
} 

然後我刪除的.xib和IBOutlet中的視圖控制器。

現在我可以用[tableView reloadData];重新加載表格視圖。我不需要.xib。

+0

是的,如果你不需要,不要使用.xib或IB。它使事情變得更加困難。 – 2012-03-29 22:38:19

相關問題