objective-c UIViewController [Xcode 5] 中的静态表视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19110668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static table view inside UIViewController [Xcode 5]
提问by Constantin Jacob
I'm aware of the problem that one is not able to have static table view content in a UIViewController in
我知道无法在 UIViewController 中拥有静态表格视图内容的问题
I don't get a warning/error but he also doesn't compile. Is there a trick to it or do I have to use the old ways around it?
我没有收到警告/错误,但他也没有编译。它有什么技巧还是我必须使用旧方法?
Thanks in advance.
提前致谢。
回答by Can Poyrazo?lu
UPDATE: With the latest update (Xcode 5.1) it seems that it's no longer possible to put static cells inside regular UIViewController. My answer still applies for UITableViewController though.
更新:随着最新的更新(Xcode 5.1),似乎不再可能将静态单元格放入常规 UIViewController 中。不过,我的答案仍然适用于 UITableViewController。
Yes, you can have static table view content in UIViewController.
是的,您可以在 UIViewController 中拥有静态表格视图内容。
All you need to do is:
您需要做的就是:
-Create the table's static cells in interface builder and design them the way you like.
- 在界面生成器中创建表格的静态单元格并按照您喜欢的方式设计它们。
-Make the UIViewController implement table view's data source and delegate:
- 使 UIViewController 实现 table view 的数据源和委托:
@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
-Connect the table view's delegate and dataSource to the view controller in interface builder
- 将表视图的委托和数据源连接到界面构建器中的视图控制器
-Implement -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionto return the number of your cells. (e.g. return 10, yes simple as that)
--(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section实现返回您的单元格数量。(例如return 10,是的,就这么简单)
-Connect your cells to your code as IBOutlets in Interface Builder. IMPORTANT:Make sure they are strong, weak won't work. e.g. @property (strong, nonatomic) IBOutlet UITableViewCell *myFirstCell;
- 在 Interface Builder 中将您的单元格作为 IBOutlets 连接到您的代码。重要提示:确保它们是strong,弱将不起作用。例如@property (strong, nonatomic) IBOutlet UITableViewCell *myFirstCell;
-Implement -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathto return the correct cell at index path. e.g:
- 实现-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath在索引路径返回正确的单元格。例如:
int num = indexPath.row;
UITableViewCell *cell;
switch (num) {
case 0:
cell = self.myFirstCell;
break;
case 1:
cell = self.mySecondCell;
break;
}
return cell;
If you apply all these steps, you should have working static cells that works for tables with not many cells. Perfect for tables that you have a few (probably no more than 10-20 would be enough) content. I've ran the same issue a few days ago and I confirm that it works. More info on my answer here: Best approach to add Static-TableView-Cells to a UIViewcontroller?
如果您应用所有这些步骤,您应该拥有适用于单元格不多的表格的工作静态单元格。非常适合您有一些(可能不超过 10-20 个就足够了)内容的表格。几天前我遇到了同样的问题,我确认它有效。关于我的答案的更多信息:Best approach to add Static-TableView-Cells to a UIViewcontroller?
回答by Dannie P
There's a way to improve Can's answer.
有一种方法可以改进 Can 的答案。
Connect your cells to code not as IBOutletbut as IBOutletCollection. If you name it as e.g. cells your code will look like this, which makes it slightly cleaner:
将您的单元格连接到代码,而不是IBOutlet作为IBOutletCollection. 如果您将其命名为例如单元格,您的代码将如下所示,这会使其更清晰:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.cells[indexPath.row];
}
The order in which you connect cells to outlet collection will be the order you see when run the app.
您将单元格连接到插座集合的顺序将是您在运行应用程序时看到的顺序。
I can think of supporting several sections by linking their cells to several outlet collections.
我可以考虑通过将它们的单元格链接到几个出口集合来支持多个部分。
回答by k06a
This is my try:
这是我的尝试:


I have created container view and Table View Controller. Then I opened source code of Storyboard and changed destination identifier of container view to table view container identifier. Now make table view controller static...
我已经创建了容器视图和表视图控制器。然后我打开了 Storyboard 的源代码并将容器视图的目标标识符更改为表视图容器标识符。现在使表视图控制器静态...
UPDATE:
更新:
Just Ctrl+Drag from ContainerView to UITableViewController!
只需 Ctrl+从 ContainerView 拖动到 UITableViewController!
UPDATE 2:
更新 2:
Set embedded view controller class to smith like MYStaticTableViewController, witch should only have this method to provide -prepareForSeguecalling to parent view controller:
将嵌入式视图控制器类设置为 smith like MYStaticTableViewController,女巫应该只有这个方法来提供-prepareForSegue对父视图控制器的调用:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([self.parentViewController respondsToSelector:@selector(prepareForSegue:sender:)])
[self.parentViewController prepareForSegue:segue sender:sender];
}
UPDATE 3:
更新 3:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if ([self.parentViewController respondsToSelector:@selector(shouldPerformSegueWithIdentifier:sender:)])
return [self.parentViewController shouldPerformSegueWithIdentifier:identifier sender:sender];
return YES;
}
回答by etipton
Can's solution does break in XCode 5.1 :(
Can 的解决方案在 XCode 5.1 中确实失效了 :(
I found a workaround which builds off the same basic idea, but unfortunately requires a little more involvement: http://www.codebestowed.com/ios-static-tableview-in-uiviewcontroller/
我找到了一种基于相同基本思想的解决方法,但不幸的是需要更多的参与:http: //www.codebestowed.com/ios-static-tableview-in-uiviewcontroller/
To summarize, you can add TableViewCells directly to views (and create IBOutlets from them, etc), but in order for them to get "moved" to the TableView properly, you need to remove them from the view in code, and you also need to set Auto-Layout constraints in IB.
总而言之,您可以将 TableViewCells 直接添加到视图中(并从中创建 IBOutlets 等),但是为了让它们正确地“移动”到 TableView,您需要在代码中将它们从视图中删除,并且您还需要在 IB 中设置自动布局约束。
回答by Peter R.
As Dannie P mentioned above, using an IBOutletConnection is the way to go. To clarify on this a bit further:
正如 Dannie P 上面提到的,使用 IBOutletConnection 是要走的路。为了进一步澄清这一点:
Take the first cell from your static table view and ctrl+drag it into your UITableViewController. On the connection property window, select Outlet Collection on the Connection pull down menu.
从静态表视图中取出第一个单元格,然后按住 ctrl+将其拖入 UITableViewController。在连接属性窗口中,选择连接下拉菜单上的插座集合。
Your should end up with code similar to this:
您应该最终得到与此类似的代码:
@property (strong, nonatomic) IBOutletCollection(UITableViewCell) NSArray *cells;
Next, ctrl+drag over all the rest of your cells (one at a time) onto the property you created above in the order you want them to appear in your static table view.
接下来,按您希望它们出现在静态表视图中的顺序,按住 ctrl+将所有其余单元格(一次一个)拖到您在上面创建的属性上。

