UI_UITableView 编辑与移动

The UI of iPhone Contacts (Day 10)

创建一个Person类,声明以下属性

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *phoneNumber;
@property (nonatomic, copy) NSString *introduce;

创建字典存储Person数据
RootViewController.m

@property (nonatomic, retain) NSMutableDictionary *allDataDict;

//#pragma mark - 加载数据
- (void) loadData // 类似addAllViews添加所有控件的功能
{
    // 1. 创建person对象
    Person *p1 = [Person personWithName:@"Xiao Huang Gua" phoneNumber:@"11248374342" introduce:@"I'm little cuke."];
    Person *p2 = [Person personWithName:@"Xiao Bai Gua" phoneNumber:@"21243348333" introduce:@"I'm little white."];
    Person *p3 = [Person personWithName:@"Li Li" phoneNumber:@"31246456743" introduce:@"I'm Lily."];
    Person *p4 = [Person personWithName:@"Qiang Ge" phoneNumber:@"41242353743" introduce:@"I'm strong."];
    Person *p5 = [Person personWithName:@"Wang Zong" phoneNumber:@"51248665645" introduce:@"I'm BOSS."];

    // 2. 创建字典
    self.allDataDict = [NSMutableDictionary dictionary];

    // 3. 添加内容
    [_allDataDict setObject:[NSMutableArray arrayWithObjects:p1, p2, nil] forKey:@"X"];
    [_allDataDict setObject:[NSMutableArray arrayWithObjects:p3, nil] forKey:@"L"];
    [_allDataDict setObject:[NSMutableArray arrayWithObjects:p4, nil] forKey:@"Q"];
    [_allDataDict setObject:[NSMutableArray arrayWithObjects:p5, nil] forKey:@"W"];

}

先设置数据源 _rootView.tableView.dataSource = self;
然后实现数据源协议中的三个方法
记得在loadView调用加载数据[self loadData];

//#pragma mark - 添加全部控件
- (void)addAllViews
{
    self.tableView = [[[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain] autorelease];
    [self addSubview:_tableView];
}
//#pragma mark - 显示数据的三个方法
//#pragma mark 设置分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _allDataDict.count;
}

// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 先通过section获取key值
    NSString *key = _allDataDict.allKeys[section];
    // 再通过key值获取数组
    NSMutableArray *mutableArray = _allDataDict[key];
    // 返回数组长度
    return  mutableArray.count;

    // 后两步可合写为 return [_allDataDict[key] count];
}

// 设置显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
    }

    // 设置内容
    // 先通过indexPath.section获取key值
    NSString *key = _allDataDict.allKeys[indexPath.section];
    // 通过key值去除数组
    NSMutableArray *mutableArray = _allDataDict[key];
    // 从数组中取出想要显示的Person对象
    Person *p = mutableArray[indexPath.row];
    // 显示
    cell.textLabel.text = p.name;
    cell.detailTextLabel.text = p.introduce;


    return cell;
}

// 设置title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _allDataDict.allKeys[section];

}

// 设置快速索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _allDataDict.allKeys;
}



/*  ***编辑状态四步*** 
   ***0. 进入编辑状态***  
   ***1. 设置可以编辑***
   ***2. 设置编辑样式***
   ***3. 处理编辑过程*** */
----------------------------------------------------

- (void)viewDidLoad {
 /*...*/
 // 0. 进入编辑状态
 // 右上角添加按钮
 // 语法糖
    self.navigationItem.rightBarButtonItem = ({

        UIBarButtonItem *editBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(editBarButtonItemAction:)] autorelease];
        editBarButtonItem;

    });
}

//#pragma mark - 编辑的方法
- (void)editBarButtonItemAction:(UIBarButtonItem *)sender
{
    if ([sender.title isEqualToString:@"编辑"]) {
        sender.title= @"完成";
    } else {
        sender.title = @"编辑";
    }

    // 进入和退出编辑
    [_rootView.tableView setEditing:!_rootView.tableView.editing animated:YES];
}

// 1. 设置可以编辑
//#pragma mark - 编辑
//#pragma mark 设置是否允许编辑(以下只允许第四组编辑)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        return YES;
    } else {
        return NO;
    }    
}

// 2. 设置编辑状态(记得设置代理并遵守协议)
//#pragma mark 设置编辑状态
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleInsert;
}

// 3. 处理编辑过程
//#pragma mark 处理编辑过程 ※※ (在点Delete时执行)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    // 处理数据
    // 1. 根据indexPath.section找到key
    NSString *key = _allDataDict.allKeys[indexPath.section];
    // 2. 根据key值,找到数组
    NSMutableArray *mutableArray = _allDataDict[key];
    // 3. 判断数组中元素个数
    if (mutableArray.count == 1) {
        // 4.1 根据key值删除键值对
        [_allDataDict removeObjectForKey:key];
        // 4.2 更新页面(删除组)
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
    } else {
        // 5.1 根据下标删除元素
        [mutableArray removeObjectAtIndex:indexPath.row];
        // 5.2 更新页面(删除行)
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}


一个修改Delete显示内容的方法

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"放手吧";
}

移动

0. 进入编辑状态
1. 设置是否可以移动
2. 处理移动过程
3. 限制跨分组移动

//#pragma mark - 移动过程
//#pragma mark 设置是否可以移动(以下只有第三组可移动)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        return YES;
    } else {
        return NO;
    }
}

//#pragma mark 处理移动过程
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
  // 1. 根据section获取key值
    NSString *key = _allDataDict.allKeys[sourceIndexPath.section];

    // 2. 根据key值获取数组
    NSMutableArray *mutableArray = _allDataDict[key];

    // 3. 根据下标找到人(加retain是为了防止内存释放)
    Person *p = [[mutableArray[sourceIndexPath.row] retain] autorelease]; 

    // 4. 删除人
    [mutableArray removeObject:p];

    // 5. 插入人
    [mutableArray insertObject:p atIndex:destinationIndexPath.row];

    // 6. 更新页面(在此方法中可以省略)
    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}


// 3. 限制跨分组移动
//#pragma mark 跨分组移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    // 判断目标位置和源位置是否在一个分区
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        // 如果是,则可以移动,就返回目标位置
        return proposedDestinationIndexPath;
    } else {
        // 否则,返回源位置,不允许移动
        return sourceIndexPath;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值