// 可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.currentIndex < 2) {
return YES;
}
return NO;
}
// 设置进入编辑状态 cell不会缩进
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// 添加action
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
// iOS系统小于11则使用常规方法
if(iOS_VERSION.floatValue < 11.0) {
__weak __typeof__(self) weakSelf = self;
// 历史数据和标准数据
if (self.currentIndex < 2) {
// 删除一条
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
__strong FileChildViewController *strongSelf = weakSelf;
[strongSelf deleteOne:indexPath];
}];
deleteAction.backgroundColor = BaseRedColor;
// 上传一条
UITableViewRowAction *uploadAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"上传" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
__strong FileChildViewController *strongSelf = weakSelf;
[strongSelf uploadOne:indexPath];
}];
if (self.currentIndex == 0) {
// 设置为标准数据
UITableViewRowAction *savetoStandardAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:LOCALIZATION(@"设置为标准数据") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
__strong FileChildViewController *strongSelf = weakSelf;
[strongSelf saveToStandard:indexPath];
}];
[savetoStandardAction setBackgroundColor:[UIColor blueColor]];
return @[deleteAction,uploadAction,savetoStandardAction];
} else {
// 设置为对比数据
UITableViewRowAction *comparisonAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:LOCALIZATION(@"设为对比数据") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
__strong FileChildViewController *strongSelf = weakSelf;
[strongSelf setToComparision:indexPath];
}];
[comparisonAction setBackgroundColor:[UIColor blueColor]];
return @[deleteAction,uploadAction,comparisonAction];
}
}
return nil;
} else {
return nil;
}
}
// 只适用于ios11 以及以后版本
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) {
if (@available(iOS 11.0, *)) {
if (self.currentIndex < 2) {
__weak __typeof__(self) weakSelf = self;
// 删除数据
UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
__strong FileChildViewController *strongSelf = weakSelf;
[strongSelf deleteOne:indexPath];
completionHandler(YES);
}];
[deleteAction setBackgroundColor:BaseRedColor];
// 上传数据
UIContextualAction *uploadAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"上传" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
__strong FileChildViewController *strongSelf = weakSelf;
[strongSelf uploadOne:indexPath];
completionHandler(YES);
}];
if (self.currentIndex == 0) {
// 设为标准数据
UIContextualAction *standardAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:LOCALIZATION(@"设置为标准数据") handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
__strong FileChildViewController *strongSelf = weakSelf;
[strongSelf saveToStandard:indexPath];
completionHandler(YES);
}];
[standardAction setBackgroundColor:[UIColor blueColor]];
// 添加
UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction,uploadAction,standardAction]];
actions.performsFirstActionWithFullSwipe = NO;
return actions;
} else {
// 设置为对比数据
UIContextualAction *comparisionAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:LOCALIZATION(@"设为对比数据") handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
__strong FileChildViewController *strongSelf = weakSelf;
[strongSelf setToComparision:indexPath];
completionHandler(YES);
}];
[comparisionAction setBackgroundColor:[UIColor blueColor]];
// 添加
UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction,uploadAction,comparisionAction]];
actions.performsFirstActionWithFullSwipe = NO;
return actions;
}
} else {// 云端数据暂时无
return nil;
}
}
return nil;
}
编辑tableview, editActionsForRowAtIndexPath
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...