xcode 将 UIPickerView 添加到 UITableViewCell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9690013/
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
Adding UIPickerView to a UITableViewCell
提问by Gaurav_soni
I want to add a UIPickerView to a UITableViewCell. Right now I am just getting a black window. Can someone also explain me the concept of subclassing UI objects to a cell ie:when do we make our cell as the delegate and the datasource delegate? Thanks
我想将 UIPickerView 添加到 UITableViewCell。现在我只是得到一个黑色的窗口。有人还可以向我解释将 UI 对象子类化为单元格的概念,即:我们什么时候将单元格作为委托和数据源委托?谢谢
EDIT: Here is the code of what i am doing right now
编辑:这是我现在正在做的代码
case 1: {
cell = [tableView dequeueReusableCellWithIdentifier:@"groups"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groups"];
}
/*NSString *myGroupDetail = [_groupArray objectAtIndex:indexPath.row];
NSLog(@"the group detail label is %@",myGroupDetail);
cell.textLabel.text = myGroupDetail;
*/
[cell addSubview:_groupPicker];
break;
}
The groups is the section in which I want the picker view, I am getting that data from an array.
组是我想要选择器视图的部分,我从数组中获取数据。
回答by Sebastian Flückiger
you would have to allocate and initialize a pickerview in your cellForRow method of the tableviewdelegate. ill sketch it for you =) how to initialize a cell itself should not be hard to find out if you google a bit ;-)
您必须在 tableviewdelegate 的 cellForRow 方法中分配和初始化一个选择器视图。我会为你画草图 =) 如果你用谷歌搜索一下,如何初始化一个单元格应该不难发现;-)
...(tableView *)... cellForRowAtIndexPath... {
if(indexPath.row == pickerRow){
UIPickerView *pickerView = [[UIPickerView alloc]init];
cell = ... // alloc and initialize a cell
cell addSubview:pickerView];
}
else{ // your other cells }
return cell;
}
UPDATE:im currently having trouble with git, so i uploaded a sample project to my private server: UITablePicker example
更新:我目前在使用 git 时遇到问题,所以我上传了一个示例项目到我的私人服务器:UITablePicker 示例
github: https://github.com/sebamisc/UItableViewWithPicker
github: https://github.com/sebamisc/UItableViewWithPicker
yu can modify and use the code however you want =)
你可以随意修改和使用代码=)
sebastian
塞巴斯蒂安
回答by Hermann Klecker
Well, I never did exactly that.
好吧,我从来没有这样做过。
Does it have to be in a cell or could you use the table's header or footer view for that? (I would not suggest a section header/footer view.)
它是否必须在单元格中,或者您可以使用表格的页眉或页脚视图吗?(我不建议部分页眉/页脚视图。)
Assuming it is within a UITableViewCell. Yes, I personally would subclass UITableViewCell. Did that a lot. In that case you cell object could be the data source delegate of the picker. For doing so your subclass of UITableViewCell (let's assume you name it MyTableViewCell) needs to fulfil the related protocol. You add that UIPickerView programmatically within the init Method (initWithStyle) of MyTableViewCell. For the layout within the table cell, you should overwrite the method layoutSubviews. If your app can be rotated to landscape and portrait orientations and/or if your app is designed to run on iPad as well, then this method should dynamically consider the table's bounds. (Screen or windwo bounds are often used hiere but that is not save when the table is displayed within a split master view or a popup view on iPads.)
假设它在 UITableViewCell 中。是的,我个人会继承 UITableViewCell。做了很多。在这种情况下,您的单元格对象可能是选择器的数据源委托。为此,您的 UITableViewCell 子类(假设您将其命名为 MyTableViewCell)需要满足相关协议。您可以在 MyTableViewCell 的 init 方法 (initWithStyle) 中以编程方式添加 UIPickerView。对于表格单元格内的布局,您应该覆盖方法 layoutSubviews。如果您的应用程序可以旋转到横向和纵向方向和/或如果您的应用程序也设计为在 iPad 上运行,则此方法应动态考虑表格的边界。(屏幕或窗口边界通常用于 hiere 但当表格显示在 iPad 上的拆分主视图或弹出视图中时不会保存。)
Strictly spoken your MyTableViewCell should not be the data source delegate, simply because it is a view element and view objects are not supposed to manage any business logic within an MVC design pattern. Smarter would be the implementation of some dedicated view controller for your table view cell hat fulfills the protocol and is assigned as the delegate. However, both would work. In the event that it is the only picker view within your sell, then you could easily use your UITableViewController subclass even without tagging the UIPickerView.
严格地说,你的 MyTableViewCell 不应该是数据源委托,因为它是一个视图元素,视图对象不应该管理 MVC 设计模式中的任何业务逻辑。更聪明的是为您的表格视图单元格帽子实现一些专用的视图控制器,以满足协议并被分配为委托。但是,两者都可以。如果它是您销售中唯一的选择器视图,那么即使不标记 UIPickerView,您也可以轻松使用您的 UITableViewController 子类。
An alternative to subclassing a UITableViewCell is to create the UIPickerView within the cellForRowAtIndexPath method of your tableViewController. That is fine for singe-orientation apps. However, you may setup it in a way that it re-arranges its the UIPickerView automatically.
子类化 UITableViewCell 的另一种方法是在 tableViewController 的 cellForRowAtIndexPath 方法中创建 UIPickerView。这对于单一方向的应用程序来说很好。但是,您可以通过自动重新排列 UIPickerView 的方式设置它。
In any case you should overwrite the heightForRowAtIndexPath method of UITableViewController when your table views do not have the same hight any more. If all of them still have the same height, then you can simply set the rowHeight property of your UITableView.
在任何情况下,当您的表格视图不再具有相同的高度时,您应该覆盖 UITableViewController 的 heightForRowAtIndexPath 方法。如果它们仍然具有相同的高度,那么您可以简单地设置 UITableView 的 rowHeight 属性。