ios - 如何向原型(prototype)单元格添加两个以上的标签?

标签 ios uitableview xcode5

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

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

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

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

有什么办法吗?

最佳答案

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

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

  2. 返回 Interface Builder,转到 Storyboard中的 TableView 并确保它有一个单元格原型(prototype)。 (如果它没有从对象库中拖一个到 TableView 。)

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

    • 在 Storyboard的单元格“属性”检查器中,将单元格“ Storyboard标识符”设置为您将在第 5 步中引用的内容(我在这里使用了 CustomCell);

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

      Custom

    • 将标签添加到单元格。


    我在此处为单个原型(prototype)单元格添加了标签:

    enter image description here

  3. 使用“助理编辑器”将您的代码与 Storyboard同时显示。选择您添加到场景中的标签之一,将下面的代码更改为您在第 1 步中创建的 UITableViewCell 子类,您现在可以control-drag从标签创建标签到单元格的自定义子类的 IBOutlet 引用:

    enter image description here

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

  4. 现在您的 tableview Controller 可以引用这个子类:

    - (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 引用即可。

关于ios - 如何向原型(prototype)单元格添加两个以上的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23746279/

相关文章:

ios - 删除动态调整 TableView 的行?

ios - 如何删除 xCode 5 中的配置文件

64-bit - Xcode 5 在运行 64 位单元测试时显示 "Simulator in Use"

objective-c - NSDate 日期方法返回错误结果

ios - 动画 AVPlayerLayer 变换属性

ios - tableview单元格子 ScrollView 不水平滚动

ios - iOS 中的 DBL_EPSILON 问题

ios - MVVMCross 绑定(bind)属性在 iOS 模拟器中工作正常,但在设备上失败

ios - 如何以编程方式更改此元素在 ViewController 上的位置?

iphone - UIGestureRecognizer 阻止表格单元格 View 中的按钮