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

UITableView编辑 增删改查

2015-08-10 22:28 676 查看
@interface RootTableViewController ()

@property (nonatomic , retain) NSMutableArray *dataArray;
//用数组管理表视图将要显示的数据

@end

@implementation RootTableViewController

//新的重用机制格式
//1.定义全局区的静态重用标识符
static NSString *identifier = @"CELL";

//重写属性的getter方法
- (NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [[NSMutableArray alloc]init];
}
return _dataArray;
}

- (void)viewDidLoad {
[super viewDidLoad];
//给数据源数组赋值
[self.dataArray addObject:@"1"];
[self.dataArray addObject:@"2"];
[self.dataArray addObject:@"3"];
[self.dataArray addObject:@"4"];

//2.此方法是为对应的重用标识符注册单元格的类型,用于配合iOS6.0提供的新的出队列方法,我们应该先注册,再使用..----UITableViewCell这个是系统样式的Cell,如果以后用到了自定义Cell,在这里可以换上自定义cell的类名
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
//editButtonItem对应的响应方法内部会根据点击按钮的状态通过setEditing:animtated:方法来控制表视图是否进入编辑状态
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//这是第三步
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
//forIndexPath:  这个参数,再使用if(cell == nil)判断的时候,不要写。

cell.textLabel.text = self.dataArray[indexPath.row];

// Configure the cell...

return cell;
}

// Override to support conditional editing of the table view.
//第一步:询问能否进入编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
//重写此协议方法可以让表视图根据条件进行编辑,如果是某一行不想被编辑,可以通过判断来return NO;

//此协议方法是第一个被调用的,当表视图进入编辑状态的时候通过此方法的返回值来确定对应行是否进入编辑状态

if (indexPath.row == 0) {
return NO;
}
return YES;
}

//指定单元格的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.row == self.dataArray.count - 1) {
return UITableViewCellEditingStyleInsert;
}
//也是为对应的行指定其编辑格式
return UITableViewCellEditingStyleDelete;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//第二步:通过deleagte协议方法为对应行指定其编辑格式
if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//1.如果删除数据,需要先删除对应行的数据
//2.再将对应行的单元格从表视图中删除
//开始删除
[self.dataArray removeObjectAtIndex:indexPath.row];
//然后开始删除单元格
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
//1.先在数组中插入对应的对象
//2.创建对应对象的indexPath
//3.根据indexPath在表视图中插入行

//开始
[self.dataArray addObject:@"2"];
NSIndexPath *insterPath = [NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0];//这里面的数据都是下标,从0开始

[tableView insertRowsAtIndexPaths:@[insterPath] withRowAnimation:UITableViewRowAnimationTop];

}
}

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//当通过拖拽将对应行移动到目标位置时会响应此方法,该方法是通过两个对应的indexPath去修改数据源数组中的数据
//具体实现<内存平衡>
id targetObj = [self.dataArray [fromIndexPath.row] retain];
[self.dataArray removeObject:targetObj];
[self.dataArray insertObject:targetObj atIndex:toIndexPath.row];
[targetObj release];
NSLog(@"%@" , self.dataArray);

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

return YES;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: