iOS 15 UITableView 分组高度显示异常处理方案

3,603 阅读1分钟

在 iOS15 中UITableView新增了属性有:

/// Determines if the table view allows its cells to become focused.
/// When tableView:canFocusRowAtIndexPath: is implemented, its return value takes precedence over this method.
/// Defaults to a system derived value based on platform and other properties of the table view.
@property (nonatomic, getter=isPrefetchingEnabled) BOOL prefetchingEnabled
/// The height for filler rows added below the last row when there aren't enough rows to fill a plain style table view.
/// Set 0 to disable filler rows entirely, use `UITableViewAutomaticDimension` for the default height.
@property (nonatomic) CGFloat fillerRowHeight
/// Padding above each section header. The default value is `UITableViewAutomaticDimension`.
@property (nonatomic) CGFloat sectionHeaderTopPadding
/// Determines if the table view allows its cells to become focused.
/// When tableView:canFocusRowAtIndexPath: is implemented, its return value takes precedence over this method.
/// Defaults to a system derived value based on platform and other properties of the table view.
@property (nonatomic) BOOL allowsFocus
/// Determines if the table view allows its cells to become focused while editing.
/// When tableView:canFocusRowAtIndexPath: is implemented, its return value takes precedence over this method.
/// Defaults to a system derived value based on platform and other properties of the table view.
@property (nonatomic) BOOL allowsFocusDuringEditing

方法:

// Reconfigures any existing cells for the rows. Reconfiguring is more efficient than reloading a row, as it does not replace the
// existing cell with a new cell. Prefer reconfiguring over reloading unless you actually need an entirely new cell for the row.
- (void)reconfigureRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths 
/// Determines if the row at the specified index path should also become selected when focus moves to it.
/// If the table view's global selectionFollowsFocus is enabled but this delegate returns NO, focus will still move to the cell upon selection, but the cell will no longer become selected when focus moves to it.
- (BOOL)tableView:(UITableView *)tableView selectionFollowsFocusForRowAtIndexPath:(NSIndexPath *)indexPath

其中属性值sectionHeaderTopPadding, 设sectionHeaderTopPadding为 0即可。

  • 部分页面设置
if (@available(iOS 15.0, *)) {
    _tableView.sectionHeaderTopPadding = 0;
}
  • 全局设置
if (@available(iOS 15.0, *)) {
    [UITableView appearance].sectionHeaderTopPadding = 0;
}