您的位置:首页 > 产品设计 > UI/UE

UitableView的增删和移动

2015-10-24 20:31 363 查看
//声明tableview并初始化Array
-(void)viewDidLoad{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 50, 300, 350)];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];

NSMutableArray *dataArray = [[NSMutableArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7", nil];
}

//协议的重写
#pragma mark - UITableViewDataSource Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Identifier = @"identifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc ]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:Identifier];
}
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
return  cell;
}

//对选中的行进行标记,在不编辑的状态下使用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];
if (cellView.accessoryType == UITableViewCellAccessoryNone) {
cellView.accessoryType=UITableViewCellAccessoryCheckmark;
}
else {
cellView.accessoryType = UITableViewCellAccessoryNone;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}

//在进行下面的编辑之前先将列表设置为可编辑状态
[tableView setEditing:NO];

//实现列表的移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;//可移动
}

//实现移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//    需要的移动行
NSInteger fromRow = [sourceIndexPath row];
//    获取移动某处的位置
NSInteger toRow = [destinationIndexPath row];
//    从数组中读取需要移动行的数据
id object = [dataArray objectAtIndex:fromRow];
//    在数组中移动需要移动的行的数据
[dataArray removeObjectAtIndex:fromRow];
//    把需要移动的单元格数据在数组中,移动到想要移动的数据前面
[dataArray insertObject:object atIndex:toRow];
}

//实现列表的编辑

//可编辑状态(增加行,逐行删除)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;  //设置为YES可滑动进入编辑状态
}

//单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;//返回编辑风格:  -:删除; +:添加  无:不可编辑
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete) {
//        通过获取的索引值删除数组中的值
[dataArray removeObjectAtIndex:indexPath.row];
//        删除单元格的某一行时,在用动画效果实现删除过程
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//动画:8种
}
if(editingStyle==UITableViewCellEditingStyleInsert)
{
//增加行
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: