如何向原型单元添加两个以上标签?

2023-11-22

我已经完成了下面的教程并且效果很好。我的问题是如何将两个以上的标准单元添加到原型单元中?

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

cell.textLabel.text = "标题文本"; cell.detailTextLabel.text = "详细文本"

我想添加另外 4 个标签,并想使用故事板来布置它们。

有什么想法如何做到这一点?


您可以使用自定义单元格类型,并且可以添加任意数量的标签:

  1. 创建一个空的UITableViewCell您将用于该单元的子类。请注意,该子类不需要在其内部添加任何代码@implementation。我们只会为其属性添加销售点,这些将显示在其属性中@interface,但故事板消除了为单元本身编写任何代码的需要。

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

    • 在右侧的“身份”检查器面板上,将单元原型的基类设置为您的UITableViewCell子类作为单元原型的“基类”;

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

    • 将单元格“样式”设置为“自定义”而不是“基本”或“详细”:

      Custom

    • 将标签添加到单元格中。


    我已在此处将标签添加到单个原型单元中:

    enter image description here

  3. 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:

    enter image description here

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

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

    - (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非常简单,只需引用IBOutlet您在 Interface Builder 中连接的引用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何向原型单元添加两个以上标签? 的相关文章

随机推荐