如何创建自定义设置视图/使单元格标签可编辑
我需要创建应用程序中使用的自定义设置视图。该应用程序是一个模型,因此它实际上已经有了一个模型,它使用 UITableViewCellStyleValue1,但没有任何内容是可编辑的。所以本质上我想要的是同样的东西,但是使用可编辑的detailTextLabel(右侧的标签),最好的方法是什么?
I need to create a custom settings view used within the app. The app is a mockup so it actually does have one already, it uses a UITableViewCellStyleValue1, but nothing is editable. So essentially what I would like to have is the same thing but with editable detailTextLabel (the label on the right hand side), what's the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以继续使用 UITableViewCellStyleValue1。您需要为每个想要编辑的单元格创建 UITextField 实例。然后,将每个 UITextField 分配给相应单元格的accessoryView 属性。
您还可以通过以下方式匹配标签文本的颜色:
您可能需要调整 UITextField 实例的frame 属性以获得正确的对齐方式。
You can continue to use UITableViewCellStyleValue1. You'll need to create UITextField instances for each cell you want editable. Then you assign each UITextField to the appropriate cell's accessoryView property.
You can also match the color of the label text via:
You may need to fiddle with the frame property of the UITextField instances to get the alignment just right.
我认为更简单的方法是使用detailTextLabel来显示文本并处理行选择代码中文本编辑字段的显示和隐藏。我有以下工作:
以及取消选择的以下内容
唯一的其他技巧是将 cellForRowAtIndexPath 中的行选择突出显示设置为无...
I think that the easier way of doing this is to use the detailTextLabel to show the text and handle the show and hide of the text edit fields in the row selection code. I have this working with the following:
and the following on the de-select
The only other trick was to set the row selection highlighting in the cellForRowAtIndexPath to none...
好的,这是我现在的代码,textField 没有显示,我不知道为什么......
}
OK, so here's the code I have now, the textField is not displayed, I'm not sure why...
}
您的文本字段未显示,因为您从未设置文本字段的框架。您需要将框架设置为正宽度和高度。我使用的高度是 43(行高 - 1px 灰线缓冲区)。
Your text field is not showing up because you never set the frame of the text field. You need to set the frame with a positive width and height. The height I would use is 43 (row height - 1px grey line buffer).
啊哈!好的,我明白了!现在我想做的是通过查看单元格中左侧标题标签的宽度来动态定义详细信息文本字段的宽度。大多数时候我在模拟器中运行它,标题标签大小为 0,但偶尔我会得到结果。这对我来说没有任何意义,我在想也许是因为细胞被重复使用等等?你们有什么想法吗?
我在设置 textLabel 之后添加了这段代码:
然后在开头的 case 块中我执行以下操作:
Aha! OK, I got it! Now what I'm trying to do is dynamically define the width of the detail text field by looking at the width of the title label left in the cell. Most of the times I run this in the simulator the title label size is 0, but occasionally I get a result. This doesn't make any sense to me, I'm thinking maybe it's because cells are being reused or so? Do you guys have any ideas?
I added this piece of code after textLabel is set:
And then in the case block at the beginning I do: