iPhone:如何构建类似于联系人地址单元格的自定义表格视图单元格
这可能已经讨论过,但我找不到问题。
我需要在表格视图中显示/编辑地址。查看 iPhone 联系人应用程序中的地址单元格,似乎他们使用单个单元格来显示和编辑地址。
我首先认为这是通过使用 4 个自定义单元格来编辑地址来完成的,但我看不到地址类型字段如何跨越 4 行,此外,当选择时,它选择了所有 4 行。这让我认为它是通过单个表视图单元完成的,该单元具有 UItextField 的自定义排列。
目前我正在走这条路。目前让我陷入困境的是处理单元格之间的灰线。我最初打开了 UItextField 边框,但这看起来不太好。所以我正在考虑进行定制绘图。但这似乎并没有绘制在单元格的顶部。
有谁知道苹果如何构建这个地址单元?
谢谢
this may have been discussed already but I cannot find the question.
I need to display/edit an address in a table view. Looking at the address cell in the iPhones contacts app, it appears that they use a single cell for both displaying and editing the address.
I first thought it was done by using 4 custom cells to edit the address, but I could not see how the address type field could span the 4 rows, in addition, when selected, it selected all 4 rows. This made me think it was done with a single table view cell, which has a custom arrangement of UItextFields.
Currently I'm going down this path. The thing that has me stuck at the moment is dealing with the grey lines between cells. I originally turned on the UItextField borders, but that didn't really look very good. So I'm looking into doing custom drawing. But that appears to not draw on top of the cell.
Does anyone have any idea how apple constructs this address cell?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望表视图中的每个单元格之间有一条线,请使用“分组”样式 - 请参阅 Interface Builder 中属性检查器中的“表视图”选项卡。每个组的成员之间都有界线。
您是否考虑过使用 UITableViewCells 而不是以编程方式添加 UIView?您可以轻松地向这样的 XIB 添加标签和文本字段 + 垂直线。这对我来说非常有效,使用以下代码:
以及
相应的 UITableView 方法
最好的问候,托马斯。
If you want a line between each cell in a table view, then use the style 'grouped' - see the Table View tab in Attributes Inspector in Interface Builder. Members of each group will have lines between them.
Did you consider using UITableViewCells instead of programmatically adding UIViews? You can easily add a label and a text field + vertical line to such a XIB. This works really well for me with the following code:
and
with the corresponding UITableView method
Best regards, Thomas.
执行以下操作:
UITableViewCellStyleValue2
样式cell.detailsTextLabel
的numberOfLines
属性设置为您选择的数字-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
方法并返回使用以下公式计算的高度:44.0f + (numberOfLines - 1) * 19.0f
我在互联网上的某个地方读过这篇文章,但不记得出处。今天刚尝试过,效果完美。
Do the followings:
UITableViewCellStyleValue2
style for your cellnumberOfLines
properties ofcell.detailsTextLabel
to a number of your choosing-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
method and return the height calculated using this formula:44.0f + (numberOfLines - 1) * 19.0f
I read this somewhere on the Internet, but don't remember the source. Just tried this today, and it works flawlessly.
我已经有了一些东西,但我不确定它是否构建得那么好。我基本上创建了一个 UIView,它保存地址各个字段的 UITextFields。我为其构建了一个自定义控制器,用于管理数据并在字段之间绘制灰线,就像联系人中的灰线一样。
然后我将此自定义 UIView 添加到 UITableView 单元格中。我的理解是,这是推荐的方法,因为在 drawRect: 中进行的任何绘图都会保留在 UITableViewCell 的边界内。我在 UITableView 单元格类中使用 drawRect: 进行的实验在 UITableView 的背景上绘制了线条,而不是单元格。
这基本上是有效的,但是有一点我不喜欢。为了让灰线延伸到 UITableViewCell 的边框,我必须确保放置在其中的 UIView 的大小完全相同,以便在 drawRect: 中完成的任何绘图都可以直接延伸到 UITableViewCell 的边缘。
但是,如果我使用不透明的背景,我会失去单元格的漂亮圆角,因为它是在顶部绘制的。所以我不得不使用透明背景。我的理解是,出于性能原因,建议使用不透明背景,因此我不确定我是否真的正确构建了此控件。幸运的是,屏幕上不会再有超过 2 或 3 个。
I've got something working, but I'm not sure if its built that well. I basically created a UIView which holds the UITextFields for the individual fields of an address. I built a custom controller for it that manages the data and draws the gray lines between the fields just like the one in contacts.
I then added this custom UIView to the UITableView cell. My understanding is that this is the recommended way to do this because any drawing in drawRect: then stays within the boundaries of the UITableViewCell. My experiments with using drawRect: in the UITableView cell class drew lines on the background of the UITableView, not the cell.
This basically works, however there is one thing I don't like. To get the gray lines to extend to the border of the UITableViewCell I had to ensure that the UIView I placed in it was exactly the same size so that any drawing done in drawRect: could extend right to the edge of the UITableViewCell.
However if I use a opaque background I loose the nice rounded corners of the cell because it is drawn over the top. So I had to use a transparent background. My understanding is that opaque backgrounds are recommended for performance reasons so I'm not sure if I'm really building this control correctly. Luckily there will not be any more that 2 or 3 on a screen.