iOS15系统下UITableView列表中Settings文本点击后消失问题求助
iOS15 UITableViewCell文本消失问题排查与解决方案
问题根因
该异常是iOS15对UITableViewCell的内容渲染逻辑变更导致的:
- iOS15起系统默认使用
UIListContentConfiguration管理cell内容样式,如果你继续使用传统的textLabel属性且没有显式指定文本颜色,系统会根据cell的高亮、选中状态以及表格的整体交互状态自动调整textLabel的文本颜色,你看到的“文本消失”本质是文本颜色被调整为和cell背景色一致导致肉眼不可见。 - 只有“Settings”行出现异常,是因为iOS15对系统语义匹配的常用字符串,会触发更高优先级的状态颜色自动调整规则,其他普通字符串不会命中该特殊逻辑,因此仅这一行出现问题。
可行解决方案
你可以根据业务需求任选以下一种方案修复:
- 方案1(改动手最小):在cell初始化阶段显式指定
textLabel的固定文本颜色,修改后的cell初始化代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FLTAccountTVCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLTAccountTVCellReuseId]; if (!cell) { cell = [[FLTAccountTVCell alloc] initWithReuseIdentifier:kFLTAccountTVCellReuseId]; cell.textLabel.font = [FLTStylesheet regularFontWithSize:kFontSizeMedium]; // 新增一行,指定固定文本颜色,替换为你业务实际需要的颜色即可 cell.textLabel.textColor = [UIColor darkTextColor]; } cell.textLabel.text = [self titleForCellAtIndexPath:indexPath]; return cell; }
- 方案2:如果不需要cell选中高亮效果,直接在初始化时设置cell的选中样式为无即可规避自动颜色调整逻辑:
cell.selectionStyle = UITableViewCellSelectionStyleNone; - 方案3(适配iOS15新规范):针对iOS15及以上系统使用官方推荐的
UIListContentConfiguration配置cell内容,兼容性更好:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FLTAccountTVCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLTAccountTVCellReuseId]; NSString *title = [self titleForCellAtIndexPath:indexPath]; if (!cell) { cell = [[FLTAccountTVCell alloc] initWithReuseIdentifier:kFLTAccountTVCellReuseId]; } if (@available(iOS 15.0, *)) { UIListContentConfiguration *config = cell.defaultContentConfiguration; config.text = title; config.textProperties.font = [FLTStylesheet regularFontWithSize:kFontSizeMedium]; config.textProperties.color = [UIColor darkTextColor]; cell.contentConfiguration = config; } else { cell.textLabel.font = [FLTStylesheet regularFontWithSize:kFontSizeMedium]; cell.textLabel.textColor = [UIColor darkTextColor]; cell.textLabel.text = title; } return cell; }
内容的提问来源于stack exchange,提问作者Steven Smith
相关产品推荐
相关产品推荐

