简体   繁体   English

如何将子视图添加到故事板中的自定义UITableViewCell

[英]How to add subviews to a custom UITableViewCell in a storyboard

In Xcode 4, you used to be able to add subviews to the contentView of a dynamic UITableViewCell prototype in a storyboard. 在Xcode 4中,您曾经能够在故事板中将子视图添加到动态UITableViewCell原型的contentView中。 I no longer seem to be able to do this--when I drag any view to the table view cell, it doesn't let me add it to the cell, only to the parent table view. 我似乎不再能够这样做 - 当我将任何视图拖动到表格视图单元格时,它不允许我将它添加到单元格,仅添加到父表格视图。 Is there a new way to do this that I don't know about yet? 有没有新方法可以做到这一点我还不知道呢?

故事板

查看层次结构

It's Xcode 5 bug. 这是Xcode 5的错误。

Cell created without Content View. 单元格创建没有内容视图。 In my case I added one more prototype cell and remove previous, in new cell content view is present. 在我的情况下,我添加了一个原型单元格并删除之前的新单元格内容视图。

It's still possible in Xcode 5. 它仍然可以在Xcode 5中使用。

I think the reason why you can't is that you are missing the contentView for your cell. 我认为你不能的原因是你错过了你的单元格的contentView。 It might be that you either accidentally deleted it or the XIB or Storyboard somehow got corrupted. 可能是你不小心删除了它,或者XIB或Storyboard以某种方式被破坏了。

Normally, your table view controller hierarchy should look like the one below. 通常,您的表视图控制器层次结构应如下所示。 You would then add your subviews to the contentView. 然后,您可以将子视图添加到contentView。

If it was me and your still this early in development, I would just recreate it, or otherwise split the cells out into their own XIBs (which also has the nice side effect of making them reusable in other view controllers) 如果是我和你仍处于开发的早期阶段,我会重新创建它,或以其他方式将单元格拆分成它们自己的XIB(这也有很好的副作用,使它们可以在其他视图控制器中重复使用)

在此输入图像描述

In Xcode 5 storyboard, if you add a Prototype cell by selecting your UITableView and clicking on Prototype Cells count option in Attribute Inspector , then for the first time the Prototype cell will be created without Content View . 在Xcode 5 storyboard中,如果通过选择UITableView并单击Attribute Inspector中的Prototype Cells count选项添加Prototype单元格 ,那么将首次创建没有Content ViewPrototype单元格

The solution for this problem is either you create two Prototype Cells and delete the first one or drag and drop Table View Cell from Object Library in to your UITableView. 解决此问题的方法是创建两个Prototype Cells并删除第一个将Table View Cell从Object Library拖放到UITableView中。 In both cases you will have TableViewCell with a Content View . 在这两种情况下,您都将拥有带有内容视图的 TableViewCell

My UITableViewCell appeared with a contentView and I still couldn't connect the IBOutlets to the subviews. 我的UITableViewCell出现一个contentView ,我仍然无法将IBOutlets连接到子视图。 I ended up deleting the parent UITableView , the UITableViewCell Prototype, and all related subviews. 我最终删除了父UITableViewUITableViewCell Prototype和所有相关的子视图。 I then re-added everything back in and the IBOutlet -connection dragging magically works. 然后我重新添加了所有内容, IBOutlet -connection拖动神奇地工作。 I think it's an Xcode bug stemming from copy-pasting a controller and/or subviews into a storyboard and/or adding the prototyped cells manually in the inspector. 我认为这是一个Xcode错误,源于将控制器和/或子视图复制粘贴到故事板中和/或在检查器中手动添加原型单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM