如何在原型单元中添加两个以上的标签? [英] How to add more than two labels to prototype cell?

查看:82
本文介绍了如何在原型单元中添加两个以上的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了下面的教程,它运行正常。我的问题是如何在原型单元格中添加两个以上的标准单元?

I have gone through the tutorial below and it works fine. My question is how do I add more than the two standard cells to the prototype cell?

http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in- ios7-json-with.html

cell.textLabel.text =Title Text;
cell.detailTextLabel.text =详细信息文本

cell.textLabel.text = "Title Text"; cell.detailTextLabel.text = "Detail Text"

我想添加另外4个标签,并希望使用故事板进行布局。

I am wanting to add another 4 labels and would like to lay them out using the storyboards.

任何想法如何做?

推荐答案

你可以使用自定义单元格类型,您可以根据需要添加任意数量的标签:

You can use a custom cell type and you'll be able to add as many labels as you want:


  1. 创建一个空 UITableViewCell 您将用于此单元格的子类。注意,这个子类在 @implementation 中不需要任何代码。我们只会为其属性添加插座,这些将显示在其 @interface 中,但故事板不需要为单元格编写任何代码,本身。

  1. Create a empty UITableViewCell subclass that you'll use for this cell. Note, this subclass doesn't need any code inside its @implementation. We're only going to add outlets for its properties, and those will show up in its @interface, but the storyboard eliminates the need to write any code for the cell, itself.

返回Interface Builder,转到故事板中的表格视图,确保它有一个单元格原型。 (如果它没有将对象库中的一个拖到表视图上。)

Back in Interface Builder, go to the table view in your storyboard and make sure it has a cell prototype. (If it doesn't drag one from the object library on to the table view.)


  • 结束身份右边的检查器面板,将单元原型的基类设置为 UITableViewCell 子类作为单元原型的基类;

  • Over on the "Identity" inspector panel on the right, set the base class of the cell prototype to be your UITableViewCell subclass as the cell prototype's "base class";

在故事板的单元格属性检查器中,将单元格故事板标识符设置为您将在步骤5中引用的内容(我已使用 CustomCell here);

In the storyboard's "Attributes" inspector for the cell, set the cell "Storyboard identifier" to something you'll reference down in step 5 (I've used CustomCell here);

将单元格Style设置为Custom而不是Basic或Detailed:

Set the cell "Style" to "Custom" rather than "Basic" or "Detailed":

将标签添加到单元格中。

add your labels to the cell.


我添加了这里标记为单个原型单元格:


I've added for labels to a single prototype cell here:

使用助理编辑器同时显示代码他的故事板。选择您添加到场景中的一个标签,将下面的代码更改为您在步骤1中创建的 UITableViewCell 子类,现在您可以控制 -drag从标签创建 IBOutlet 对单元格自定义子类的标签的引用:

Use the "Assistant Editor" to show your code simultaneously with the storyboard. Select one of the labels you've added to the scene, change the code down below to be the UITableViewCell subclass you created in step 1, and you can now control-drag from the label to create IBOutlet references for the labels to the cell's custom subclass:

顺便说一下,我建议不要使用 IBOutlet 名称 textLabel detailTextLabel (它们不仅太通用了,而且可能与标准单元格布局中出现的标签混淆)。

By the way, I'd advise against using IBOutlet names of textLabel or detailTextLabel (not only are they too generic, but it can get confused with the labels that appear in standard cell layouts).

现在你的tableview控制器可以引用这个子类:

Now your tableview controller can reference this subclass:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell"; // make sure this matches the "Identifier" in the storyboard for that prototype cell
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    // retrieve the model data to be shown in this cell

    // now fill in the four labels:

    cell.firstNameLabel.text = ...;
    cell.lastNameLabel.text  = ...;
    cell.emailLabel.text     = ...;
    cell.telephoneLabel.text = ...;

    return cell;
}


所以尽管有通过这里的几个步骤,最终的结果是你可以设计你想要的任何单元格布局,并使用这个非常简单的 UITableViewCell 子类,你的 cellForRowAtIndexPath 非常简单,仅引用您在Interface Builder中连接的 IBOutlet 引用。

So while there are a couple of steps to go through here, the net result is that you can design whatever cell layout you want, and with this very simple UITableViewCell subclass, your cellForRowAtIndexPath is incredibly simple, just referencing the IBOutlet references you connected in Interface Builder.

这篇关于如何在原型单元中添加两个以上的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆