You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中导入DropDown后ImageView无法显示图文的问题求助

Swift DropDown库自定义单元格图片不显示问题修复

问题描述

导入DropDown库后,自定义MyDropDownCell并添加logoImage的ImageView,点击按钮时下拉菜单的单元格无法正常显示图片。

核心修复步骤

1. 调整配置顺序:先完成Cell配置再显示菜单

原代码中先调用dropDown.show()再设置Cell相关属性,导致配置未生效就渲染了菜单。必须将show()放到所有配置之后。

修改后的按钮点击代码:

@objc func buttonTapped(_ sender: UIButton) {
    let array = ["uploadvalidation","trashAdd"]
    dropDown.dataSource = ["Download report","Delete"]
    
    dropDown.anchorView = sender
    dropDown.cornerRadius = 20.0
    dropDown.bottomOffset = CGPoint(x: -140 , y: 26)
    dropDown.backgroundColor = .orange
    
    // 先配置自定义Cell
    dropDown.cellNib = UINib(nibName: "MyDropDownCell", bundle: Bundle(for: type(of: self)))
    dropDown.customCellConfiguration = { (index: Index, item: String, cell: DropDownCell) -> Void in
        guard let cell = cell as? MyDropDownCell else { return }
        print("Index: \(index), Item: \(item), Image Name: \(array[index])")
        // 赋值图片
        cell.logoImage.image = UIImage(named: array[index])
        // 同步设置文本(继承自DropDownCell的textLabel)
        cell.textLabel?.text = item
    }
    
    // 最后显示下拉菜单
    dropDown.show()
    print("Cell created: \(MyDropDownCell.self)")
}

2. 检查Nib文件的关键配置

  • 确认logoImage的IBOutlet已正确连接到xib中的ImageView,无断连情况。
  • 确保xib中Cell的Class设置为MyDropDownCell,且Module选择当前项目。
  • 检查ImageView的布局约束,避免被挤压、超出Cell边界或隐藏。

3. 验证图片资源有效性

  • 确认uploadvalidation、trashAdd图片已添加到项目,且Target Membership勾选当前项目。
  • 图片名称需完全匹配(区分大小写),如果是Assets中的图片,检查分组和名称是否一致;如果是本地文件,可尝试指定Bundle路径:
    cell.logoImage.image = UIImage(named: array[index], in: Bundle(for: type(of: self)), compatibleWith: nil)
    

4. 确认DropDown库版本

旧版本库可能存在自定义Cell的兼容性问题,建议更新到最新稳定版。

内容的提问来源于stack exchange,提问作者AKA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 21:06:10