美文网首页
0701UITableView数据刷新

0701UITableView数据刷新

作者: 李昌旭 | 来源:发表于2015-07-02 00:09 被阅读0次

今天老师讲了UITableView的数据刷新,以及UITableView的编辑删除模块。

借鉴老师的笔记并加入一些自己的理解。

## 全局刷新方法(最常用)

[self.tableView reloadData];// 没什么好说的,通用型刷新方法。UITableView可视范围都可刷新,缺点是略微会消耗一些性能,不能控制具体刷新的行数组数。有时候只需要刷新一行,结果调用全局刷新刷新了n行。这个时候就可以使用局部刷新。

## 局部刷新方法

- 添加数据

// 创建NSIndexPath数组对象,并添加数据。Row代表行Section代表组

NSArray *indexPaths = @[

[NSIndexPath indexPathForRow:0 inSection:0],

[NSIndexPath indexPathForRow:1 inSection:0]

];

// 如果添加后的行数和self.tabieview对应模型数组数对不上则会报错。所以在添加的时候模型数组也需要做对应的数据添加。

[self.wineArray insertObjects:(NSArray *) atIndexes:(NSIndexSet *)]; // 如果想要将数据添加在表格最上面,则将新的数据插入至wineArray数组的小表0位置 。

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];

- 删除数据

// 创建NSIndexPath数组对象,并删除数据。Row代表行Section代表组

NSArray *indexPaths = @[

[NSIndexPath indexPathForRow:0 inSection:0],

[NSIndexPath indexPathForRow:1 inSection:0]

];

// 同样如果删除后的行数和self.tabieview对应模型数组对不上则会报错。所以在删除的时候要模型数组也需要做对应的数据删除。

[self.wineArray removeObjectsInArray:(NSArray *)];

[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];

- 更新数据(没有添加和删除数据,仅仅是修改已经存在的数据)

NSArray *indexPaths = @[

[NSIndexPath indexPathForRow:0 inSection:0],

[NSIndexPath indexPathForRow:1 inSection:0]

];

[self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];

## 左滑出现删除按钮

- 需要实现tableView的代理方法

```objc

/**

*  只要实现了这个方法,左滑出现Delete按钮的功能就有了

*  点击了“左滑出现的Delete按钮”会调用这个方法

*/

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

// 删除模型

[self.wineArray removeObjectAtIndex:indexPath.row];

// 刷新UITableView并添加左滑动画

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}

/**

*  修改Delete按钮文字为“删除”

*/

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

return @"删除";

}

## 左滑出现N个按钮

- 需要实现tableView的代理方法

/**

*  只要实现了这个方法,左滑出现按钮的功能就有了

(一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES)

*/

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

}

/**

*  左滑cell时出现什么按钮

*/

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

// 创建两个滑动后的按钮对象,设置名称和样式并绑定点击触发block

UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

NSLog(@"点击了关注");

// 收回左滑出现的按钮(退出编辑模式)

tableView.editing = NO;

}];

UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

[self.wineArray removeObjectAtIndex:indexPath.row];

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}];

return @[action1, action0];

}

## 进入编辑模式

// self.tabelView.editing = YES;

[self.tableView setEditing:YES animated:YES];

// 默认情况下,进入编辑模式时,左边会出现一排红色的“减号”按钮

## 在编辑模式中多选

// 编辑模式的时候可以多选

self.tableView.allowsMultipleSelectionDuringEditing = YES;

// 进入编辑模式

[self.tableView setEditing:YES animated:YES];

// 获得选中的所有行

self.tableView.indexPathsForSelectedRows;

相关文章

  • 0701UITableView数据刷新

    今天老师讲了UITableView的数据刷新,以及UITableView的编辑删除模块。 借鉴老师的笔记并加入一些...

  • day10-左滑删除

    数据刷新01全局刷新数据刷新02局部刷新 是什么? UITableViewDataSource 的代理方法./**...

  • day10-数据刷新02局部刷新

    数据刷新01全局刷新 reloadData - 全局刷新,屏幕能看见的cell的数据,全部再次加载. 局部刷新 :...

  • day10-滑动时自定义多个按钮

    数据刷新01全局刷新数据刷新02局部刷新左滑删除 是什么? UITableViewDelegate - 代理方法 ...

  • 03-14 全局刷新和局部刷新

    数据刷新 添加数据 删除数据 更改数据 全局刷新方法(最常用) 局部刷新方法 添加数据 删除数据 更新数据(没有添...

  • iOS 数据刷新+左滑cell

    数据刷新 添加数据 删除数据 更改数据 全局刷新方法(最常用) 局部刷新方法 添加数据 删除数据 更新数据(没有添...

  • 数据刷新

    数据刷新 添加数据 删除数据 更改数据 全局刷新方法(最常用) [self.tableViewreloadData...

  • UITableView表视图中数据的刷新操作

    全部刷新 修改、刷新某个section,即数组array中修改了某个session数据时,刷新数据则使用该语句。 ...

  • bug记录: MJRefresh循环调用刷新block

    使用MJRefresh做的刷新,上下拉刷新都正常.当没有更多数据的时候上拉刷新,一直调用接口,刷新数据. 于是看了...

  • 数据刷新

    数据刷新: ***点击某行,弹出对话框,然后修改数据,再把数据刷新到UITableView上。 1.监听每个cel...

网友评论

      本文标题:0701UITableView数据刷新

      本文链接:https://www.haomeiwen.com/subject/hptyqttx.html