ios 如何在不使用 Storyboards 的情况下在 UITableView 中使用静态单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11041272/
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 use static cells in UITableView without using Storyboards?
提问by Kyle Fox
I have an existing ViewController + xib in my project, and now I want to add a UITableView with static cells, like this:
我的项目中有一个现有的 ViewController + xib,现在我想添加一个带有静态单元格的 UITableView,如下所示:
But when I drag a UITableView onto my screen I don't have the "Content > Static" menu in the Attributes Inspector.
但是当我将 UITableView 拖到屏幕上时,属性检查器中没有“内容 > 静态”菜单。
I've tried making my controller subclass UITableViewController, but that doesn't help -- I still don't get the option to use static cells in Attributes Inspector.
我已经尝试让我的控制器子类 UITableViewController,但这没有帮助——我仍然没有选择在 Attributes Inspector 中使用静态单元格。
I've looked around on StackOverflow but haven't found any existing answers to this question. All the existing questions relate to Storyboards (which I'm not using) instead of xib files.
我在 StackOverflow 上环顾四周,但没有找到这个问题的任何现有答案。所有现有问题都与故事板(我没有使用)而不是 xib 文件有关。
My guess is that Xcode does some kind of magic when you add a UITableViewController to a storyboard, but not when you change an existing xib to inherit from UITableViewController.
我的猜测是,当您将 UITableViewController 添加到故事板时,Xcode 会产生某种魔力,但当您将现有的 xib 更改为从 UITableViewController 继承时则不会。
Any advice how how to add a table view with static cells to an existing xib?
任何建议如何将带有静态单元格的表格视图添加到现有的 xib?
回答by Colin Humber
Static table view cells are only available when using storyboards. However, if you aren't using storyboards for your entire UI you can still use them for individual screens instead of a collection of screens.
静态表格视图单元格仅在使用故事板时可用。但是,如果您没有为整个 UI 使用故事板,您仍然可以将它们用于单个屏幕而不是屏幕集合。
To do this you can create a UIStoryboard file with a single view controller on it that has it's File's Owner set to your custom view controller subclass. Set the VC's identifier to some value. When you want to display this, get the storyboard and then instantiate your view controller subclass by creating the VC from your storyboard.
为此,您可以创建一个带有单个视图控制器的 UIStoryboard 文件,该文件的文件所有者设置为您的自定义视图控制器子类。将 VC 的标识符设置为某个值。当你想要显示它时,获取故事板,然后通过从你的故事板创建 VC 来实例化你的视图控制器子类。
UIStoryboard *tableViewStoryboard = [UIStoryboard storyboardWithName:@"your storyboard" bundle:nil];
CustomViewController = [tableViewStoryboard instantiateViewControllerWithIdentifier:@"custom identifier"];
You can present this VC as usual in your app.
你可以像往常一样在你的应用程序中展示这个 VC。
This is a great way to start using storyboards without having to convert your entire app to use them.
这是开始使用故事板的好方法,无需转换整个应用程序即可使用它们。
回答by ordinaryaverageguy
This is doable without storyboards:
这在没有故事板的情况下是可行的:
- your controller must adopt protocols for tableview delegate and source...UITableViewDelegate, UITableViewDataSource
- Add a table view to your .xib; set the table view's style to "grouped"
- Connect the table to a property in your controller (let's call it "yourTableView")
- Add table view cells to the .xib as separate views...i.e. notas subviews of the table view...they'll be free floating in the .xib. (see example image below)
- Add controls / labels etc to the table cells.
- Assign unique strings to the "Identifier" field in Attributes for each table view cell.
- Connect the table cells to other properties in the controller (sliderCell, switchCell, etc)
- Connect the send events for the cells' controls to IBAction-ed methods in the controller - (IBAction) handleSliderCell, etc.
- In the viewDidLoad method of your controller, assign the delegate and source of the table view to the controller (self.yourTableControl.delegate = self; self.yourTableControl.dataSource = self;)
- implement numberOfSectionsInTableView, numberOfRowsInSection, and cellForRowAtIndexPath in the controller
- in tableView:cellForRowAtIndexPath:, return yourFirstCell, yourSecondCell, etc pointers for appropriate indexPath values
- 您的控制器必须采用 tableview 委托和源的协议...UITableViewDelegate, UITableViewDataSource
- 将表格视图添加到您的 .xib;将表格视图的样式设置为“分组”
- 将表连接到控制器中的属性(我们称之为“yourTableView”)
- 将表格视图单元格添加到 .xib 作为单独的视图...即不是作为表格视图的子视图...它们将在 .xib 中自由浮动。(见下图示例)
- 向表格单元格添加控件/标签等。
- 为每个表格视图单元格的属性中的“标识符”字段分配唯一的字符串。
- 将表格单元格连接到控制器中的其他属性(sliderCell、switchCell 等)
- 将单元格控件的发送事件连接到控制器中的 IBAction 方法 - (IBAction) handleSliderCell 等。
- 在你的控制器的 viewDidLoad 方法中,将表视图的委托和源分配给控制器(self.yourTableControl.delegate = self; self.yourTableControl.dataSource = self;)
- 在控制器中实现 numberOfSectionsInTableView、numberOfRowsInSection 和 cellForRowAtIndexPath
- 在 tableView:cellForRowAtIndexPath: 中,返回 yourFirstCell、yourSecondCell 等指针以获取适当的 indexPath 值
Events for controls for the table cells should go to your handler routines...
表格单元格控件的事件应该转到您的处理程序例程...
Example for step 4:
步骤 4 的示例:
回答by thexande
To expand on BVB's answer, when it comes to number 10, here are the Swift 3 snippets for your tableview delegate methods. Be sure to add outlets to all your cells in the xib.
为了扩展 BVB 的答案,当谈到数字 10 时,这里是您的 tableview 委托方法的 Swift 3 片段。确保为 xib 中的所有单元格添加插座。
When you manually create your table view sections, remember that indexPath
is equal to a 2 dimensional array representing your table structure. for example, when indexPath
is passed to tableView(cellForRowAt indexPath: IndexPath)
is equal to [1][0]
, your cell will be placed in the first position of the second section.
当您手动创建表格视图部分时,请记住它indexPath
等于代表您的表格结构的二维数组。例如,当indexPath
传递给tableView(cellForRowAt indexPath: IndexPath)
等于 时[1][0]
,您的单元格将被放置在第二部分的第一个位置。
Values can be pulled out of indexPath
with the properties indexPath.row
and indexPath.section
. Using these values, you can manually construct your sections from your IBOutlets in whichever order you prefer.
可以indexPath
使用属性indexPath.row
和提取值indexPath.section
。使用这些值,您可以按照您喜欢的任何顺序从您的 IBOutlets 手动构建您的部分。
@IBOutlet var is_enabled: UITableViewCell!
@IBOutlet var is_public: UITableViewCell!
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch(section){
case 0:
return "Cell 1"
case 1:
return "Cell 2"
default: return ""
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("index path here", indexPath)
if(indexPath.section == 0) {
return is_enabled
}
else if(indexPath.section == 1) {
return is_public
} else {
return is_enabled
}
}