挖掘时,如何扩展和显示收集视图单元格内的标签的全长?
CollectionView中有一个单元格网格,如果单元格内的标签文本太长以至于显示我截断了尾巴。现在,当用户长时间敲击 /持有单元格时,全长文本看起来像uimenu选项。但是,如果文本太长,它再次被截断并显示在uicontextmenu中。如何解决这个问题? 我添加了CollectionView单元格中上下文菜单显示的代码片段。
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
if collectionView == headerCollectionView {
let headerText = headers[indexPath.row].label
return configureContextMenu(text: headerText)
}
else if collectionView == collectionView {
var data: [String] = []
dataOrganiser.getDataForSection(section: sections[indexPath.section], dataArray: &data)
return configureContextMenu(text: data[indexPath.item])
}
return configureContextMenu(text: "")
}
func configureContextMenu(text: String) -> UIContextMenuConfiguration {
let context = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { (action) -> UIMenu? in
let edit = UIAction(title: text, identifier: nil, discoverabilityTitle: nil, state: .off) { _ in }
return UIMenu(title: "", image: nil, identifier: nil, options: UIMenu.Options.destructive, children: [edit])
}
return context
}
There are a grid of cells in collectionView, if the label text inside a cell is too long to display i’ve truncated the tail. Now when the user long taps / holds the cell the full length text appears like a UIMenu option. But if the text is too long it is again truncated and shown in the UIContextMenu also. How to fix this?
I've added the code snippet for the context menu display in the collectionView cell.
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
if collectionView == headerCollectionView {
let headerText = headers[indexPath.row].label
return configureContextMenu(text: headerText)
}
else if collectionView == collectionView {
var data: [String] = []
dataOrganiser.getDataForSection(section: sections[indexPath.section], dataArray: &data)
return configureContextMenu(text: data[indexPath.item])
}
return configureContextMenu(text: "")
}
func configureContextMenu(text: String) -> UIContextMenuConfiguration {
let context = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { (action) -> UIMenu? in
let edit = UIAction(title: text, identifier: nil, discoverabilityTitle: nil, state: .off) { _ in }
return UIMenu(title: "", image: nil, identifier: nil, options: UIMenu.Options.destructive, children: [edit])
}
return context
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要更多信息来回答您的问题。可能还有一些代码。
但是,假设添加了长按手势,则您可能想调用一个更改单元格上
numberOflines
属性的函数。假设您正在使用UICollection ViewDiffableDataSource< e节,item>
,您需要更新item
(您的单元格之间应该有一些通信,并且item
> ),以便数据源看到item
已更改。然后,它显示更改。有些人采用另一种方法,他们只创建两个单元格,其中一个仅显示一条线,另一行
numberOflines
将其定为零。然后,视图控制器(如果您关注MVC或视图模型,则在关注MVVM时)将在两个单元格之间进行切换。I need more information to answer your question. Probably some code as well.
But assuming the long press gesture is added to the cell, you probably want to call a function that changes the
numberOfLines
property on the cell. Assuming you're usingUICollectionViewDiffableDataSource<Section, Item>
, you need to updateitem
(there should be some communication between your cell and theitem
), so that the datasource sees theitem
, which is hashable, has changed. Then, it displays the changes.Some people take another approach where they just create two cells in which one shows only one line and the other one sets
numberOfLines
to zero. Then, the view controller (if you're following MVC, or the view model if you're following MVVM), switches between the two cells.