首页 文章

在故事板中连接Cell Prototype的插座

提问于
浏览
36

我是故事板的新手,所以我有一些困难......

我已经创建了一个TableViewController,我想自定义Cell Prototype . 在Cell Prototype中,我添加了几个标签,我想用自己的类定制,它继承自UITableViewCell(AreaListCell) . 在Storyboard中,对于Cell Prototype,我已将Custom Class配置为“AreaListCell”,其样式为“Custom” .

在故事板中,当我选择Cell Prototype然后选择助手时,助手会显示我实现UITableViewController(AreasTableViewController)的类而不是
我的"AreaListCell"班 .

结果是我可以创建插座(使用Ctrl Drag从Cell Prototype的标签)到AreasTableViewController类,但不能创建到AreaListCell类!知道如何将Cell Prototype与我的AreaListCell类连接起来吗?

谢谢你的帮助!

3 回答

  • 21

    UPDATE: As of Xcode 4.6 (possibly earlier) you can now create outlets by control-dragging! - This has to be done into an interface section or class extension (the class extension doesn't exist by default for new cell subclasses. Thanks to Steve Haley for pointing this out.

    您无法通过拖动助手编辑器中的代码块来自动连接和创建插座,这很差,但您可以手动创建插座然后连接它们 .

    在您的单元子类接口中:

    @interface CustomCell : UITableViewCell
    
    @property (nonatomic) IBOutlet UILabel* customLabel;
    
    @end
    

    在实现中正常合成 .

    在故事板中,选择单元格并转到连接检查器,您将看到新的插座 . 从那里拖动到原型中的相关元素:

    enter image description here

    现在可以在 cellForRowAtIndexPath: 方法中将其作为 cell.customLabel 进行访问 .

  • 127

    是的,您无法使用ctrl拖动方法连接自定义原型单元格内的视图 . 而是使用视图的tag属性,然后在构建单元格时使用其标签拉出标签 .

    这里:

    //Let's assume you have 3 labels.  One for a name, One for a count, One for a detail
    //In your storyboard give the name label tag=1, count tag=2, and detail tag=3
    
    
    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CustomTableViewCell *theCell = [tableView dequeueReusableCellWithIdentifier:@"Prototype Cell"];
    
        UILabel *nameLabel = (UILabel *)[theCell viewWithTag:1];
        UILabel *countLabel = (UILabel *)[theCell viewWithTag:2];
        UILabel *detailLabel = (UILabel *)[theCell viewWithTag:3];
    
        nameLabel.text = @"name";
        countLabel.text = @"count";
        detailLabel.text = @"details";
    
        return theCell;
    }
    

    您还可以在自定义单元代码中将标签设置为属性,然后在初始化单元格时使用viewWithTag调用将标签属性分配给您在故事板上创建的标签 .

    我花了几天时间才意识到我无法从自定义单元格内部拖动来创建IBOutlet .

    祝好运!

    编辑:您可以为自定义单元格内的标签创建IBOutlets并创建链接programatticaly,而不是通过ctrl拖动方法 .

    编辑2:我完全错了,你可以按住Ctrl键 . 请参阅此问题的第二个答案 . 这很棘手,但效果很好 .

  • 0

    Swift 3

    //如果你的图像在服务器上,我们正在使用它 .

    //我们从网址获取图片 .

    //你可以从你的Xcode设置图像 .

    • 图像的URL在数组名称=缩略图中,即self.thumbnail [indexPath.row]
      在UITableviewCell上

    • 将一个imageView放在单元格上

    • 选择UIimageView从故事板中为其分配标签 .

    let pictureURL = URL(string: self.thumbnail[indexPath.row])!
    let pictureData = NSData(contentsOf: pictureURL as URL)
    let catPicture = UIImage(data: pictureData as! Data)
    var imageV = UIImageView()
    imageV = cell?.viewWithTag(1) as! UIImageView
    imageV.image = catPicture
    

相关问题