【问题标题】:How do I get a reference to the UITableView in the view controller?如何在视图控制器中获取对 UITableView 的引用?
【发布时间】:2012-03-30 06:20:19
【问题描述】:

我有一个显示可以购买的产品列表的应用。列表的视图控制器如下所示:

@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;

InAppPurchaseListCustomUIViewUIView 的子类,否则为空类。

还有一个与视图控制器同名的 .xib。 .xib 似乎只包含一个表格视图。

列表显示正常,但现在我想更改显示的项目。我有一个NSMutableArray 支持表。当我更改数组时,我需要让UITableView 用新数组重绘,但_inAppPurchaseListTable 始终为零。

  • 如何获得对 UITableView 的引用,以便调用reloadData
  • 无论如何我可以摆脱 .xib 吗?我看不出一个额外的文件除了保存一个表格视图之外什么都不做的意义。

连接检查员

【问题讨论】:

  • 您是否将_inAppPurchaseListTable 与xib 文件连接!?
  • 在 .xib 中,使用 Connections Inspector,您将哪些网点链接到表视图? ...尤其是在“参考网点”部分?另外,initWithItemList: 方法是什么样的?
  • @Scar,我该怎么做?
  • @Phillip,我附上了屏幕截图。
  • 你为什么不直接使用UITableViewController

标签: objective-c ios cocoa-touch uitableview


【解决方案1】:

我终于明白了。答案在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。

【讨论】:

  • 是的,如果没有必要,不要使用 .xib 或 IB。这让事情变得更加困难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
相关资源
最近更新 更多