iOS-UI-UITableView 的编辑模式-插入-删除-

#import "RootViewController.h"
#import "Student.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableDictionary *stuInfoDic;
@property (nonatomic, retain) UITableView *table;
@end

@implementation RootViewController

  • (void)dealloc
    {
    [_stuInfoDic release];
    [super dealloc];
    }
  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"YIem";
    // TableView
    self.table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.rowHeight = 70;
    [self.view addSubview:self.table];
    [_table release];
    [self gatData];

#warning 编辑 1 - 创建编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;

}
#warning 编辑 2 - 关联tableView编辑功能

  • (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
    [super setEditing:editing animated:animated];
    [self.table setEditing:editing animated:animated];
    }
    #warning 编辑 3 - 设置是否允许编辑
  • (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return YES;
    }
    #warning 编辑 4 - 设置编辑样式(无/ 删除/ 插入)
  • (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 按区
    // 举例: 前4个区删除样式 中间4各区插入 剩下无状态
    if (indexPath.section < 4) {
    return UITableViewCellEditingStyleDelete;
    }
    if (indexPath.section >= 4 && indexPath.section < 8) {
    return UITableViewCellEditingStyleInsert;
    }
    return UITableViewCellEditingStyleNone;
    }

#warning 编辑 5 - 实现编辑逻辑 点击编辑实际功能时触发 并且可以左滑动编辑状态

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
    {
    NSLog(@"YIEM");
//// 先操作数据   // 可以放在里面
NSString *key = [self.stuInfoDic.allKeys objectAtIndex:indexPath.section];
NSMutableArray *arr = [self.stuInfoDic objectForKey:key];


// 删除分支
if (UITableViewCellEditingStyleDelete == editingStyle) {
    //// 先操作数据

// NSString *key = [self.stuInfoDic.allKeys objectAtIndex:indexPath.section];
// NSMutableArray *arr = [self.stuInfoDic objectForKey:key];
// Student *stu = [arr objectAtIndex:indexPath.row];
// 删除学生信息
[arr removeObjectAtIndex:indexPath.row];

    //// 后操作界面
    //    [self.table reloadData]; // 整体刷新
    // 删除界面某一行
    [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    // 删除区
    

    
}
// 插入分支
if (UITableViewCellEditingStyleInsert == editingStyle) {
    
    Student *stu = [[Student alloc] init];
    stu.name = @"全❤强";
    stu.phoneNumber = @"101010101010";
    stu.picture = @"image27.jpg";//////
    // 插入数据
    [arr insertObject:stu atIndex:indexPath.row];
    // 操作界面
    [self.table insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    
    
    
}

}

yiem

yiem
yiem
yiem
yiem
yiem

标签: none

添加新评论

Title - Artist
0:00