码迷,mamicode.com
首页 > 移动开发 > 详细

进击的UI--------------- UITableView&UITableView的移动

时间:2015-11-21 18:18:23      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

1.UITableView补充 
1??:-(void)p_data
@property (nonatomic,retain)NSMutableArray *dataArray;
@property (nonatomic,assign)UITableViewCellEditingStyle editStyle;
- (void)p_data{
    NSMutableArray *arr1 = @[@"范冰冰",@"宋慧乔",@"周迅"].mutableCopy;
    NSMutableArray *arr2 = @[@"bangbangbang",@"boomshakalaka",@"comeonboys",@"comeongirls"].mutableCopy;
    self.dataArray = [NSMutableArray array];
    [self.dataArray addObject:arr1];
    [self.dataArray addObject:arr2];
}
2??:number
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
3??:row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section] count];
}
cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row];
2.UITableView修改
1??:让tableView处于编辑状态
- (void)p_navigation{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"SET" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"ADD" style:UIBarButtonItemStyleDone target:self action:@selector(leftAction:)];
}
- (void)leftAction:(UIBarButtonItem *)sender{
    self.editStyle = UITableViewCellEditingStyleInsert;
    [self.rv.tableView setEditing:!self.rv.tableView.editing animated:YES];
}
- (void)rightAction:(UIBarButtonItem *)sender{
    self.editStyle = UITableViewCellEditingStyleDelete;
    [self.rv.tableView setEditing:!self.rv.tableView.editing animated:YES];
}
2??:指定可以编辑的行
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
3??:指定tableView编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        return self.editStyle;
}
4??:完成编辑
①:修改数据流
1.[self.dataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
2.[self.dataArray[indexPath.section] insertObject:@"New Peole" atIndex:indexPath.row + 1];
②:修改 UI
1[self.rv.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
2.NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
   [self.rv.tableView insertRowsAtIndexPaths:@[newIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
③:完整代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
       if (self.editStyle == UITableViewCellEditingStyleDelete) {
         // 1.修改数据源
    [self.dataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
    // 2.修改UI
    [self.rv.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
       }else if (self.editStyle == UITableViewCellEditingStyleInsert){
           // 添加
           [self.dataArray[indexPath.section] insertObject:@"New Peole" atIndex:indexPath.row + 1];
           NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
           [self.rv.tableView insertRowsAtIndexPaths:@[newIndex] withRowAnimation:UITableViewRowAnimationAutomatic];}
3.UItableView移动
1??:让tableView处于编辑状态
同修改
2??:制定可以移动的行
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
3??:完成移动
①:修改数据源
// 拿出数据
    NSString *temp = self.dataArray[sourceIndexPath.section][sourceIndexPath.row];
    // 从数组中删掉
    [self.dataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    // 插入到指定的位置
    [self.dataArray[destinationIndexPath.section] insertObject:temp atIndex:destinationIndexPath.row];
②:修改UI
// 修改UI move方法
    [self.rv.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
③:完整代码:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    // 拿出数据
    NSString *temp = self.dataArray[sourceIndexPath.section][sourceIndexPath.row];
    // 从数组中删掉
    [self.dataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    // 插入到指定的位置
    [self.dataArray[destinationIndexPath.section] insertObject:temp atIndex:destinationIndexPath.row];
    // 修改UI move方法
    [self.rv.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
④:不建议限制跨区域
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else {
        return sourceIndexPath;
    }
}
4.UITableViewController
1??: 先注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
2??:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = @"123";
    return cell;
}

进击的UI--------------- UITableView&UITableView的移动

标签:

原文地址:http://www.cnblogs.com/sharkHZ/p/4984112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!