ios 在 Storyboard 中创建自定义 UITableViewCell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7863775/
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
Creating custom UITableViewCell's within a Storyboard
提问by Doug
Wanting to create a static menu (IOS 5) and attempting to create custom cells within the storyboard to then load onto the grouped tableview.
想要创建一个静态菜单(IOS 5)并尝试在故事板中创建自定义单元格,然后加载到分组的 tableview 上。
I've created the outlet
我已经创建了出口
@property(nonatomic,strong) IBOutlet UITableViewCell *labelCell;
The ViewController class is set to the proper TableViewController and I've connected the custom cell to this outlet.
ViewController 类设置为正确的 TableViewController,我已将自定义单元连接到此插座。
I also have the delegate and datasource set up.
我还设置了委托和数据源。
I've got
我有
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.labelCell;
}
I'm sure there is a ton wrong with this, but I'm just trying to display one cell and go from there. There does not seem to be any examples of doing custom cells within the IB through the storyboard. I can still use the old way of creating a xib file and loading it in the mainBundle but I just want to stay up to date I guess.
我确定这有很多问题,但我只是想显示一个单元格并从那里开始。似乎没有任何通过故事板在 IB 中进行自定义单元格的示例。我仍然可以使用创建 xib 文件并将其加载到 mainBundle 的旧方法,但我想我只是想保持最新状态。
but with what I have above i get a crash when I load this view controller. SIGABRT
但是根据我上面的内容,当我加载这个视图控制器时我会崩溃。SIGABRT
回答by David Casseres
Here is what I've learned about how you get cells for your table when using the storyboard. When you drag a UITableView into your view, it comes with a prototype cell already set as a subview. To use this prototype cell, set a unique reuse identifier in the attributes inspector, and then use the same identifier to dequeue the cell in your cellForRowAtIndexPath: method. I leave out the code for creating a cell from scratch if the dequeue call returns nil; I don't think it can happen. So just dequeue the cell, configure it with the usual UITableViewCell methods, and return it.
这是我在使用故事板时如何获取表格单元格的知识。当您将 UITableView 拖入您的视图时,它带有一个已经设置为子视图的原型单元格。要使用这个原型单元格,请在属性检查器中设置一个唯一的重用标识符,然后在 cellForRowAtIndexPath: 方法中使用相同的标识符使单元格出列。如果出队调用返回 nil,我会省略从头开始创建单元格的代码;我认为这不可能发生。所以只需将单元出列,使用通常的 UITableViewCell 方法配置它,然后返回它。
But you can also create custom subclasses of UITableViewCell. Just set the class name in the storyboard's class identity inspector, and drag whatever elements you want from the Objects palette into your cell. Then create IBOutlet properties for them in your subclass's code files, and hook them up to the cell in the storyboard in the usual way. This is so much better than having to do it all in code!
但是你也可以创建 UITableViewCell 的自定义子类。只需在故事板的类标识检查器中设置类名称,然后将您想要的任何元素从“对象”面板拖到您的单元格中。然后在子类的代码文件中为它们创建 IBOutlet 属性,并以通常的方式将它们连接到故事板中的单元格。这比必须在代码中完成所有工作要好得多!
And finally, you can have more than one kind of cell in your table. Just drag UITableViewCell objects from the palette into the table, and give each one a unique reuse identifier in the attributes inspector. In your cellForRowAtIndexPath: method, choose the type of each cell and you can have a very flexible table view.
最后,您的表格中可以有不止一种单元格。只需将 UITableViewCell 对象从调色板拖到表格中,并在属性检查器中为每个对象提供一个唯一的重用标识符。在您的 cellForRowAtIndexPath: 方法中,选择每个单元格的类型,您就可以拥有一个非常灵活的表格视图。
回答by Mark Adams
If you have set your UITableView
to be using 'Static Cells' in the storyboard, you don't need to implement any of the UITableViewDataSource
methods and you can modify the cell directly in Interface Builder. For a single label cell, select the cell and change it's type to 'Basic'. You can now edit the cell just like you would any other view object.
如果您已将UITableView
故事板设置为使用“静态单元格”,则无需实现任何UITableViewDataSource
方法,您可以直接在界面生成器中修改单元格。对于单个标签单元格,选择该单元格并将其类型更改为“基本”。您现在可以像编辑任何其他视图对象一样编辑单元格。
回答by SirRupertIII
Thistutorial was helpful to me. You can reference whatever object you need through the tag.
这个教程对我很有帮助。您可以通过标签引用您需要的任何对象。
In the Storyboard drag on a UIImageView
or UILabel
etc. and set the tag to 100 (whatever you want) then in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
use the tag to reference it.
在 Storyboard 中拖动 aUIImageView
或UILabel
etc. 并将标签设置为 100(无论你想要什么),然后在你- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
使用标签来引用它。
Here is the example code in the tutorial, just remember to set the tags in the storyboard:
下面是教程中的示例代码,记得在storyboard中设置标签:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:recipe.imageFile];
UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.name;
UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
recipeDetailLabel.text = recipe.detail;
return cell;
}