2012-03-29 75 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

相关问题