#pragma Mark 左滑按钮 iOS8以上
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
//添加一个删除按钮
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//先删数据 再删UI
[self.dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];
//添加一个置顶按钮
UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// [self.dataArray exchangeObjectAtIndex:0 withObjectAtIndex:indexPath.row];
TyunModel *model = self.dataArray[indexPath.row];
[self.dataArray removeObjectAtIndex:indexPath.row];
[self.dataArray insertObject:model atIndex:0];
[tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
// [tableView reloadData];
// });
[tableView setEditing:NO];
}];
topAction.backgroundColor = [UIColor blueColor];
//添加一个编辑按钮
UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"修改" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
TyunModel *model = self.dataArray[indexPath.row];
//弹窗输入名字
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改" message:@"请输入新名字" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"此处输入名字";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[tableView setEditing:NO];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *textfield = alert.textFields.firstObject;
NSString *newName = textfield.text;
if (newName == nil) {
newName = @"";
}
model.name = newName;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}]];
[self presentViewController:alert animated:YES completion:nil];
}];
editAction.backgroundColor = [UIColor greenColor];
return @[deleteAction, topAction, editAction];
}
iOS tableview系统左滑删除 左滑置顶 自定义左滑事件功能 输入框弹出
最新推荐文章于 2023-09-01 11:22:00 发布