【1】拖动好界面
【2】设置协议,数据源
【3】代码
1.声明可变数组,用来存放所有数据对象
@interface ViewController () @property(nonatomic,strong)NSMutableArray *mydata; @end
2.初始化数据【创建30个对象数据】
- (void)viewDidLoad
{
[super viewDidLoad];
self.mydata=[NSMutableArray array];
for(int i =0;i<30;i++)
{
NSString *str=[NSString stringWithFormat:@"it-%d",i];
[self.mydata addObject:str];
}
}
3.设置返回行数
#pragma mark -返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.mydata.count;
}
1.添加固定方法,显示一个cell就调用该方法,优化性能
#pragma mark 每当有以个cell进入视野范围内就会调用,返回当前这行显示的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1. 用static 修饰的 局部变量,只会初始化一次
static NSString *ID=@"Cell";
// 2. 拿到一个标识先去缓存池中查找对应的Cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
// 3. 如果缓存池中没有,才需要传入一个标识创建新的Cell
if(cell==nil)
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
// 4.覆盖数据
cell.textLabel.text=self.mydata[indexPath.row];
cell.detailTextLabel.text=@"哈哈哈";
return cell;
}
2.这个方法是提交编辑时候调用【点击Delete,就会调用下面的方法】
#pragma mark 提交编辑时调用 // 一点击删除,就会调用这个方法/ 左滑动删除 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"-------commit ------%d",indexPath.row); // 删除数据 // 1. 更改数据(删除本行数据) [self.mydata removeObjectAtIndex:indexPath.row];// 传入一个位置,将这个位置上的数据删除 // 2.刷新数据 // [tableView reloadData];// 数据刷新,全局刷 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }
3.调用代理方法中的监听事件,【删除】信息内容的修改
[开启编辑状态]
self.tableView.editing=YES; |
// 编辑状态 |
[self.tableView setEditing:YSE animated:YES]; |
// 带有动画效果 |
#pragma mark -代理方法
#pragma mark -监听item的点击
#pragma mark 删除
-(IBAction)remove
{
bool edt=self.tableView.editing; //取得现在编辑状态是否开启
//按一下开启编辑状态
// 没有动画效果
// self.tableView.editing=YES; // 编辑状态
[self.tableView setEditing:!edt animated:YES]; // 带有动画效果
}
【1】.增加方法与删除方法类似,先创建“+”按钮的代理方法,连线
【2】.代码
1.添加数据用的方法: [self.mydata insertObject:[添加的内容] atIndex:[添加到第几行]];
2.刷新数据: inSection 表示是组号,indexPathForRow表示的是行号
1)获取到一个数组
NSIndexPath *newPath=[NSIndexPath indexPathForRow:[行号] inSection:[组别号]];
2)传入一个数组
[tableView insertRowsAtIndexPaths:@[newPath] 【上面的数组】withRowAnimation:UITableViewRowAnimationTop【动画效果】];
#pragma mark 提交编辑时调用(删除和添加方法都会调用该方法)
// 一点击“+”就会调用这个方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ // 增加数据
// 1. 更改数据(删除本行数据)
[self.mydata insertObject:@"新添加数据"atIndex:indexPath.row+1];
// 2.刷新数据
NSIndexPath *newPath=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
[tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView reloadRowsAtIndexPaths:<#(NSArray *)#>【指定行】 |
刷新指定行的数据(个数不变) |
[tableView deselectRowAtIndexPath:<#(NSIndexPath *)#> 【指定行】 |
删除指定行(删除后个数与数据个数保持一致) |
[tableView insertRowsAtIndexPaths:@[newPath]【指定行要求数组】 |
插入新的行 |
3.加入add方法
-(IBAction)add
{
// 取出当前的编辑状态
bool edt=self.tableView.editing;
// 设置调用方法,方便开启编辑模式判断是否是添加
self.tableView.tag=UITableViewCellEditingStyleInsert;
// 开启编辑模式
[self.tableView setEditing:!edt animated:YES];
}
4.通过上面【add】方法与【remove】方法中设置的Tag,可以获取调用移除还是增加方法。然后使用这个编辑模式。
#pragma mark -代理方法
#pragma mark 当tableView开启编辑模式就会调用
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 通过调用add 和remove方法中的tag变量,来确定是要添加还是删除。
return tableView.tag;
}
5.如果增加的话,最终会调用一下下面的方法,但是上面已经写了
|
1
2
3
4
|
#pragma mark 每当有以个cell进入视野范围内就会调用,返回当前这行显示的cell-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{} |
1.增加代码
sourceIndexPath :当前需要移动的行
destinationIndexPath :移动目标的行
#pragma mark 如果实现了这个方法,就有排序功能
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 当从某一行一动到另外一行时,会调用该方法。
NSLog(@"%d-->%d",sourceIndexPath.row,destinationIndexPath.row);// 这行可以说明调用步骤
// 如果直接使用这样的方法,只是界面进行改变,数据没有改变,违反了MVC所以要用以下方法
// 1.取出即将要删除的数据
NSString *data =self.mydata[sourceIndexPath.row];
// 2.删除要一动的那一行
[self.mydata removeObject:data];
// 插入之前删除的数据到某一行
[self.mydata insertObject:data atIndex:destinationIndexPath.row];
}
【总结】
1.增加\删除
一、删除\添加: 1.开启编辑模式 [self.tableView setEditing:YES animated:YES]; 2.实现数据源的某个方法 tableView:commitEditingStyle:forRowAtIndexPath: 3.下面方法的返回值决定编辑模式是添加还是删除 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
2.排序
二、排序: 实现下面的方法即可: tableView:moveRowAtIndexPath:toIndexPath:
3.刷新界面
三、4个刷新UI界面的方法 1.添加新的行 [tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; 2.删除指定的行 [tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; 3.局部刷新指定的行 [tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; 4.整体刷新所有的行 [tableView reloadData];
浙公网安备 33010602011771号