ios 如何在嵌入 UIViewController 的 UITableView 中直观地创建和使用静态单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8945728/
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
How to visually create and use static cells in a UITableView embedded in a UIViewController
提问by StinkyDuck
I'm using XCode 4.2 and have built my UI using Storyboards. I need to create a view that has content above and below a UITableView and I can achieve this by using a UIViewController. A UITableViewController does not let you add content above or below the table. You can use the table header/footer but that doesn't work for what I would like to achieve.
我正在使用 XCode 4.2 并使用 Storyboards 构建了我的 UI。我需要创建一个在 UITableView 上下都有内容的视图,我可以通过使用 UIViewController 来实现这一点。UITableViewController 不允许您在表格上方或下方添加内容。您可以使用表格页眉/页脚,但这不适用于我想要实现的目标。
I now have a UIViewController with a UITableView embedded in it. I can adjust the height and width of the UITableView accordingly which provides me the UI layout that I am looking for.
我现在有一个 UIViewController,其中嵌入了一个 UITableView。我可以相应地调整 UITableView 的高度和宽度,这为我提供了我正在寻找的 UI 布局。
I can customize the static cells in the UITableView but when I try to build I get the following error:
我可以自定义 UITableView 中的静态单元格,但是当我尝试构建时,出现以下错误:
Illegal Configuration: Static table views are only valid when embedded in UITableViewController instances
非法配置:静态表视图仅在嵌入 UITableViewController 实例时才有效
My question is how are others getting around this? Creating a tableview with static cells and laying them out visually is very nice but apparently that is not allowed for some reason that I cannot understand. I can't switch to a UITableViewController because of my visual layout requirements.
我的问题是其他人如何解决这个问题?创建一个带有静态单元格的 tableview 并在视觉上布置它们非常好,但显然由于某种我无法理解的原因,这是不允许的。由于我的视觉布局要求,我无法切换到 UITableViewController。
Any assistance would be greatly appreciated.
任何帮助将不胜感激。
采纳答案by Canopus
You are right. In storyboard, you cannot have a tableView with static cells embedded in a viewController. One way around it (I have not tried it myself, though, so I am not sure if it works) can be that you create an instance of UITableViewController in storyboard with static cells. Add an instance of UIView to your viewController, and then programmatically load the tableView of the UITableViewController into the UIView of your viewController.
你是对的。在故事板中,您不能在 viewController 中嵌入带有静态单元格的 tableView。解决它的一种方法(不过,我自己没有尝试过,所以我不确定它是否有效)可以是您在故事板中使用静态单元格创建 UITableViewController 的实例。将 UIView 的实例添加到您的 viewController,然后以编程方式将 UITableViewController 的 tableView 加载到您的 viewController 的 UIView 中。
回答by Pedro Mancheno
You can achieve this in Xcode 4.5 and later versions, assuming your app is targeted at iOS 6+.
您可以在 Xcode 4.5 及更高版本中实现这一点,假设您的应用程序针对 iOS 6+。
In the Storyboard simply create a UIViewController with a View Container inside it's main view. Then hook up that View Container to a UITableViewController that contains static cells.
在 Storyboard 中,只需在其主视图中创建一个带有视图容器的 UIViewController。然后将该视图容器连接到包含静态单元格的 UITableViewController。
Just like this:
像这样:
You don't need a single line of code. Just control click, drag and select embed. The view controller containment is handled for you.
您不需要一行代码。只需控制单击、拖动并选择嵌入即可。视图控制器包含为您处理。
回答by Ken Toh
pmd's answer works but in the event that backward compatibility with iOS 5 is required as well, you can do the embedding programatically using the View Containment API.
pmd 的答案有效,但如果还需要向后兼容 iOS 5,您可以使用 View Containment API 以编程方式进行嵌入。
In the viewDidLoad method of your parent UIViewController:
在你的父 UIViewController 的 viewDidLoad 方法中:
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
MyTableViewController* vc =[storyboard instantiateViewControllerWithIdentifier:@"MyTableVC"];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
// ensure embedded view is aligned to top
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
vc.view.frame = frame;
[vc didMoveToParentViewController:self];
}
Don't forget to specify a storyboard ID for your UITableViewController with the static cells.
不要忘记使用静态单元格为 UITableViewController 指定故事板 ID。
回答by Patrick
I know this is an old question but I have a scrappy solution to this issue.
我知道这是一个老问题,但我对这个问题有一个零碎的解决方案。
I needed 3 static cells on a UIViewController so this is what I did:
我在 UIViewController 上需要 3 个静态单元格,所以这就是我所做的:
- Drag in some Table View Cells into your interface (not into UITableView) - add text and whatever else you need.
- Make IBOutlet properties for your cells and synthesise them.
- Drag a button and make it cover the entire cell. Set the button to type 'Custom' so it appears invisible - repeat for all cells
- Add numeric tags to your buttons
Implement the following functions. buttonDown is connected to the buttons 'Touch Down' event. buttonUp is connected to 'Touch Up Inside' AND'Touch Up Outside'
-(IBAction)buttonDown:(id)sender { if ([sender tag] == 1) { myFirstCell.selected = YES; } else if ([sender tag] == 2) { mySecondCell.selected = YES; } else if ([sender tag] == 3) { myThirdCell.selected = YES; } } -(IBAction)buttonUp:(id)sender { myFirstCell.selected = NO; mySecondCell.selected = NO; myThirdCell.selected = NO; }
- 将一些表格视图单元格拖入您的界面(而不是 UITableView) - 添加文本和您需要的任何其他内容。
- 为您的细胞制作 IBOutlet 属性并合成它们。
- 拖动按钮并使其覆盖整个单元格。将按钮设置为键入“自定义”,使其看起来不可见 - 对所有单元格重复
- 为按钮添加数字标签
实现以下功能。buttonDown 连接到按钮“Touch Down”事件。buttonUp 连接到“Touch Up Inside”和“Touch Up Outdoor ”
-(IBAction)buttonDown:(id)sender { if ([sender tag] == 1) { myFirstCell.selected = YES; } else if ([sender tag] == 2) { mySecondCell.selected = YES; } else if ([sender tag] == 3) { myThirdCell.selected = YES; } } -(IBAction)buttonUp:(id)sender { myFirstCell.selected = NO; mySecondCell.selected = NO; myThirdCell.selected = NO; }
You can do whatever else you like in the buttonDown event and use the button to go directly to a new view. I find this pretty useful.
你可以在 buttonDown 事件中做任何你喜欢的事情,并使用按钮直接进入一个新的视图。我觉得这很有用。
回答by aqs
I am not sure what you mean by static cells, but if you are trying to build the cells in IB, and then want to use it in your tableView, what you could do is in your cellForRowAtIndex
you can call loadNibNamed
passing the name of the .nib file you created for the cells as the parameter. Make sure that you have an outlet
in your viewController which maps to the cell's .nib. Try exploring in these directions if that's what you are trying to achieve
我不确定您所说的静态单元格是什么意思,但是如果您尝试在 IB 中构建单元格,然后想在您的 tableView 中使用它,您可以做的是在您cellForRowAtIndex
可以调用loadNibNamed
传递 .nib 的名称您为单元格创建的文件作为参数。确保outlet
您的 viewController 中有一个映射到单元格的 .nib。如果这就是您要实现的目标,请尝试朝这些方向探索
回答by Ash Var
You can make it dynamic and then switch of scrolling:
您可以将其设为动态,然后切换滚动:
[yourTableName setScrollEnabled:NO];