1.写这个就不写数据了。假设你的都是有数据的了。或者直接弄个可变数组也可当数据源。好就直接进入正题
首先设置编辑模式
//允许他能够编辑
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//允许排序
- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//设置编辑模式,这里我想让页面加载的时候可以删除,然后点击按钮的时候又可以添加数据。所以做了这样的判断
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(!isAdd)
{
return UITableViewCellEditingStyleDelete;
}else
{
return UITableViewCellEditingStyleInsert;
}
}
2.跟新数据包括删除。和添加数据
//改变数据的方法
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle==UITableViewCellEditingStyleDelete)
{
//删除数据
//先删除数据数组里面的数据
[self.array removeObjectAtIndex:indexPath.row];
//跟新视图
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
else if(editingStyle==UITableViewCellEditingStyleInsert)
{
//插入数据
//创建弹窗视图
UIAlertController *alertControll = [UIAlertController alertControllerWithTitle:nil message:@"添加联系人" preferredStyle:UIAlertControllerStyleAlert];
//创建弹窗按钮
UIAlertAction *btnCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *btnSure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//封装数据并且添加数据到可变数组Array
for(UITextField *txt in alertControll.textFields)
{
if([txt.text isEqualToString:@""]||txt.text==nil)
{
return ;
}
}
myModel *model = [[myModel alloc]init];
for(UITextField *txt in alertControll.textFields)
{
if(txt.tag==200)
{
model.name = txt.text;
}
else if(txt.tag==300)
{
model.tel = txt.text;
}
}
[self.array insertObject:model atIndex:indexPath.row+1];
//创建一个indexPath,由于我们插入的数据应该在后一位。所以得重新设置一下插入的位置。
NSIndexPath *myIndex = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
//视图刷新
[self.tableView insertRowsAtIndexPaths:@[myIndex] withRowAnimation:UITableViewRowAnimationMiddle];
}];
//添加输入框
[alertControll addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入联系人姓名";
textField.tag = 200;
}];
[alertControll addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入联系人电话";
textField.tag = 300;
}];
[alertControll addAction:btnCancel];
[alertControll addAction:btnSure];
//弹出弹出窗
[self presentViewController:alertControll animated:YES completion:nil];
}
}
3.改变数据顺序
//改变数据顺序,也就是当你拖动数据再那个位置的时候,然后就改变到你拖动的那个位置
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//改变数据位置
if(sourceIndexPath.row<destinationIndexPath.row)
{
for(NSInteger i=sourceIndexPath.row;i<destinationIndexPath.row;i++)
{
[self.array exchangeObjectAtIndex:i withObjectAtIndex:i+1];
}
}
else
{
for(NSInteger i = sourceIndexPath.row;i>destinationIndexPath.row;i--)
{
[self.array exchangeObjectAtIndex:i withObjectAtIndex:i-1];
}
}
}