iOS开发 - 解决尝鲜升级Xcode Version 13.0,iOS15 UITableView分组高度出现异常的问题

iOS15发布已有一周了,今日尝鲜将Xcode升级到Version 13.0版本用于适配iOS15不料有新瓜
俺的UITableView分组高度出现了异常页面变得很难看

UITableView分组高度出现异常

这明显是iOS15中UITableView又增加了新的属性或方法
prefetchingEnabled
fillerRowHeight
sectionHeaderTopPadding
allowsFocus
allowsFocusDuringEditing
- (void)reconfigureRowsAtIndexPaths:(NSArray *)indexPaths
- (BOOL)tableView:(UITableView *)tableView selectionFollowsFocusForRowAtIndexPath:(NSIndexPath *)indexPath

先看一下iOS15新增的属性和方法

新增属性:

@property (nonatomic, getter=isPrefetchingEnabled) BOOL prefetchingEnabled API_AVAILABLE(ios(15.0), tvos(15.0), watchos(8.0));
/// 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 API_AVAILABLE(ios(15.0), tvos(15.0), watchos(8.0));
/// Padding above each section header. The default value is `UITableViewAutomaticDimension`.
@property (nonatomic) CGFloat sectionHeaderTopPadding API_AVAILABLE(ios(15.0), tvos(15.0), watchos(8.0));
/// 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 API_AVAILABLE(ios(15.0), tvos(15.0));
/// 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 API_AVAILABLE(ios(15.0), tvos(15.0));

新增方法:

// 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 *)indexPaths API_AVAILABLE(ios(15.0), tvos(15.0), watchos(8.0));
/// 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, this method will allow you to override that behavior on a per-index path basis. This method is not called if selectionFollowsFocus is disabled. 
- (BOOL)tableView:(UITableView *)tableView selectionFollowsFocusForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(15.0)) API_UNAVAILABLE(watchos, tvos);

由于还没有对这些新增的方法/属性进行体验不做过多的使用解释,只说新增的sectionHeaderTopPadding属性

官方解释:

/// Padding above each section header. The default value is UITableViewAutomaticDimension.
//在每个分组头部填充一个预设间隙

那问题找到了解释这个sectionHeaderTopPadding新增属性有个默认间隙造成的

解决方案就是消除这个默认的头部间隙

方法一:

- (void)adaptateiOS15 {
    if (@available(iOS 15.0, *)) {
        //设置默认的分组头部间隙为0
        _tableView.sectionHeaderTopPadding = CGFLOAT_MIN;
    }
}

方法二(全局设置):

- (void)tableViewAdaptateiOS15 {
    if (@available(iOS 15.0, *)) {
        //设置默认的分组头部间隙为0
        [UITableView appearance].sectionHeaderTopPadding = CGFLOAT_MIN;
    }
}

设置后再运行界面美丽如初
剩余的新增方法/属性有空体验一下再做使用总结!!

你可能感兴趣的:(iOS开发 - 解决尝鲜升级Xcode Version 13.0,iOS15 UITableView分组高度出现异常的问题)