iOS14+使用UIListContentConfiguration无法显示UITableViewCell内容求助
解决UITableViewCell使用UIListContentConfiguration不显示内容的问题
你的代码逻辑本身没问题,但单元格不显示内容大概率是以下几个原因之一,对应解决方案如下:
1. 单元格未正确注册
确保在viewDidLoad中正确注册系统默认的UITableViewCell,如果是自定义Cell也要适配UIListContentConfiguration:
override func viewDidLoad() { super.viewDidLoad() tableView.register(UITableViewCell.self, forCellReuseIdentifier: K.cellIdentifier) // 如果是自定义Cell,注册自定义Cell类,且自定义Cell内部不要遮挡contentView }
2. 尝试直接创建新的内容配置而非复用默认配置
替换原iOS14+分支的代码,直接创建UIListContentConfiguration实例:
if #available(iOS 14.0, *) { var content = UIListContentConfiguration.cell() content.text = "This is a cell" // 如需副标题可设置content.secondaryText = "副标题" cell.contentConfiguration = content } else { cell.textLabel?.text = "This is a cell" }
3. 检查数据源方法返回值
确认numberOfRowsInSection方法返回正数,否则不会生成单元格:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 // 替换为你的实际数据源数量 }
4. 自定义Cell的特殊情况
如果使用自定义UITableViewCell,确保:
- 没有在
contentView上添加完全遮挡的子视图 - 自定义Cell未重写
contentConfiguration相关逻辑导致配置失效
内容的提问来源于stack exchange,提问作者Ashutosh
相关产品推荐
相关产品推荐

