前言
整理一下UITableview的cell左滑刪除的注意點,實現(xiàn)一個簡單的左滑刪除功能。整理的過程也是一個回歸的過程,有時候一些功能很久沒寫就忘記了系統(tǒng)的實現(xiàn)方法。
iOS11之前的editActionsForRowAtIndexPath方法暫時不去適配,可以自行了解去適配iOS10版本
iOS11新增的系統(tǒng)側滑方法
ios11新增的方法支持圖片和文字側滑樣式, 默認的樣式是圖片在上,文字在下?;瑒硬僮鬟@里還有一個需要注意的是,當cell高度較小時,會只顯示image,不顯示title,當cell高度夠大時,會同時顯示image和title。
// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
// return nil to get the default swipe actions
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvOS);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvOS);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) {
UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"刪除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
if (self.dataArray.count > indexPath.row) {
[self.dataArray removeObjectAtIndex:indexPath.row];
[UIView performWithoutAnimation:^{
[self.detailTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
}
completionHandler(YES);
}];
// action.image = [UIImage imageNamed:@"tab_home"];
action.backgroundColor = UIColorFromRGB(0xC9C6CC);
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[action]];
/// 控制是否可以左滑到頭刪除當前cell,默認為YES
// config.performsFirstActionWithFullSwipe = NO;
self.editingIndexPath = indexPath;
[self.view setNeedsLayout];
return config;
}
不同系統(tǒng)版本對左滑樣式的處理
/// 設置左滑菜單按鈕的樣式
- (void)setupSlideBtn {
if (@available(iOS 13.0, *)) {
for (UIView *subView in self.detailTableView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >= 1) {
UIView *remarkContentView = subView.subviews.firstObject;
[self setupRowActionViewInit: remarkContentView];
}
}
return;
}
if (@available(iOS 11.0, *)) {
for (UIView *subView in self.detailTableView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1) {
UIView *remarkContentView = subView;
remarkContentView.backgroundColor = [UIColor clearColor];
[self setupRowActionViewInit: remarkContentView];
}
}
return;
}
// iOS11 以下的版本
UITableViewCell *cell = [self.detailTableView cellForRowAtIndexPath:self.editingIndexPath];
for (UIView *subView in cell.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subView.subviews count] >= 1) {
UIView *remarkContentView = subView;
[self setupRowActionViewInit:remarkContentView];
}
}
}
自定義cell高度時左滑樣式處理
左滑刪除的系統(tǒng)樣式默認是cell的高度,當在cell顯示的View和cell本身高度不等高時候需要處理
/// 系統(tǒng)默認的側滑刪除高度是和cell等高的,修改樣式
- (void)setupRowActionViewInit:(UIView *)rowActionView {
CGRect frame = rowActionView.frame;
if (self.editingIndexPath == nil) {
if (frame.origin.y > 0) {
frame.origin.y = 0;
frame.size.height += 15*KYY;
}
} else {
if (frame.origin.y == 0) {
frame.origin.y += 15*KYY;
frame.size.height -= 15*KYY;
}
}
rowActionView.frame = frame;
UIButton *button = rowActionView.subviews.firstObject;
[button setTitle:@"刪除" forState:UIControlStateNormal];
// [button setImage:[UIImage imageNamed:@"tab_home"] forState:UIControlStateNormal];
button.titleLabel.font = UIDEFAULTFONTSIZE(16);
}