挖掘时,如何扩展和显示收集视图单元格内的标签的全长?

发布于 2025-02-04 23:33:12 字数 1290 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2025-02-11 23:33:12

我需要更多信息来回答您的问题。可能还有一些代码。

但是,假设添加了长按手势,则您可能想调用一个更改单元格上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 using UICollectionViewDiffableDataSource<Section, Item>, you need to update item (there should be some communication between your cell and the item), so that the datasource sees the item, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文