UITableView 列表视图2

_dataArray = [[NSMutableArray alloc] initWithObjects:@"乔丹", @"麦迪", @"艾弗森", @"詹姆斯", @"韦德", @"科比", @"约翰逊", nil];
    _delArray = [[NSMutableArray alloc] init];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonClick)] autorelease];
    self.navigationItem.rightBarButtonItem = button;
    
    UIBarButtonItem* leftButton = [[[UIBarButtonItem alloc] initWithTitle:@"del" style:UIBarButtonItemStyleBordered target:self action:@selector(deleteRows)] autorelease];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    
    //点击edit事件,切换tableView的编辑状态
- (void)buttonClick{
    if (_tableView.editing) {
        [_delArray removeAllObjects];
        [_tableView setEditing:NO animated:YES];
    } else {
        [_delArray removeAllObjects];
        [_tableView setEditing:YES animated:YES];
    }
}
//必写
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
//必写
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"] autorelease];
    }
    
    NSString* str = [_dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = str;
    
    return cell;
}

//编辑部分  是否能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//编辑类型  滑动删除还是点击删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert ;
}

//改变删除按钮上得文本
- (NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

//处理编辑事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果是删除的话
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //先删除数组里的,再删除tableView上对应的cell
        [_dataArray removeObjectAtIndex:indexPath.row];
        //重新加载全部数据,刷新数据
        //[tableView reloadData];
        //删除行
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
    //如果编辑类型为插入的话
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        [_dataArray insertObject:@"new" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }
}

//多选删除
- (void)deleteRows{
    //首先从数组中删除对应数据
    NSMutableArray* array = [NSMutableArray array];
    for (int i = 0; i < _delArray.count; i++) {
        NSIndexPath* indexPath = [_delArray objectAtIndex:i];
        NSString* str = [_dataArray objectAtIndex:indexPath.row];
        [array addObject:str];
        //_delArray 2 3 5 6
        //_dataArray 0 1 2 3 4 5 6 7 8
    }
    [_dataArray removeObjectsInArray:array];
    
    [_tableView deleteRowsAtIndexPaths:_delArray withRowAnimation:UITableViewRowAnimationMiddle];
    
    [_delArray removeAllObjects];
}

//选中一行调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"我选中了:%d",indexPath.row);
    if (tableView.editing) {
        //编辑状态,用户选中cell得位置添加到数组里
        [_delArray addObject:indexPath];
    }
}

//取消选中一行调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"取消选中:%d",indexPath.row);
    if (tableView.editing) {
        [_delArray removeObject:indexPath];
    }
}


你可能感兴趣的:(UITableView,列表视图2)