1.tableView的刷新

1> 数据刷新的总体步骤

* 修改模型数据

* 刷新表格(刷新界面)

2> 刷新表格(刷新界面)的方法

* 全局刷新(每一行都会重新刷新)

- (void)reloadData;

* 局部刷新(使用前提: 刷新前后, 模型数据的个数不变)

- (void)reloadRows:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

* 局部删除(使用前提: 模型数据减少的个数 == indexPaths的长度)

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

实例:

#define NJContactsPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"contacts.arc"]

@interface NJContatcsViewController ()<UIActionSheetDelegate, NJAddViewControllerDelegate, NJEditViewControllerDelegate>
/**
* 点击注销按钮
*/
- (IBAction)logout:(UIBarButtonItem *)sender; /**
* 保存所有用户数据
*/
@property (nonatomic, strong) NSMutableArray *contatcs;
@end @implementation NJContatcsViewController
- (void)viewDidLoad
{
[super viewDidLoad]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 给当前控制器的当行控制器添加一个按钮
UIBarButtonItem *addBtn = self.navigationItem.rightBarButtonItem;
UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editBtnClick)]; self.navigationItem.rightBarButtonItems = @[editBtn, addBtn];
} - (void)editBtnClick
{
// NSLog(@"editBtnClick");
// 开启tableview的编辑模式
// self.tableView.editing = !self.tableView.editing;
[self.tableView setEditing:!self.tableView.editing animated:YES];
} - (IBAction)logout:(UIBarButtonItem *)sender
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil];
[sheet showInView:self.view]; }
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ( != buttonIndex) return; // 移除栈顶控制器
[self.navigationController popViewControllerAnimated:YES];
} // 无论是手动类型的segue还是自动类型的segue, 在跳转之前都会执行该方法
// 控制器跳转之前(执行segue之前)执行
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// 0.判断目标控制器是添加还是编辑
// 1.取出目标控制器
UIViewController *vc = segue.destinationViewController;
if ([vc isKindOfClass:[NJAddViewController class]]) {
NJAddViewController *addVc = (NJAddViewController *)vc;
// 2.设置代理
addVc.delegate = self;
}else if ([vc isKindOfClass:[NJEditViewController class]]){
// 传递数据
NJEditViewController *editVc = (NJEditViewController *)vc; // 通过tableview获取被点击的行号
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
// 取出被点击行的模型
NJContatc *c = self.contatcs[path.row];
NSLog(@"联系人列表 %p" , c);
// 赋值模型
editVc.contatc = c;
// 设置代理
editVc.delegate = self;
}
} #pragma mark - NJEditViewControllerDelegate
- (void)editViewControllerDidClickSavBtn:(NJEditViewController *)editViewController contatc:(NJContatc *)cpmtatc
{
// 0.更新保存的数据
// NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"contacts.arc"];
[NSKeyedArchiver archiveRootObject:self.contatcs toFile:NJContactsPath]; // 1.刷新表格
[self.tableView reloadData];
} #pragma mark - NJAddViewControllerDelegate
- (void)addViewControllerDidAddBtn:(NJAddViewController *)editViewController contatc:(NJContatc *)contatc
{
// 1.保存数据到数组中
[self.contatcs addObject:contatc]; // 在这个地方保存用户添加的所有的联系人信息
// NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject ] stringByAppendingPathComponent:@"contacts.arc"];
[NSKeyedArchiver archiveRootObject:self.contatcs toFile:NJContactsPath]; // 2.刷新表格
[self.tableView reloadData];
} #pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.contatcs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
NJContatcCell *cell = [NJContatcCell cellWithTableView:tableView];
// 2.设置模型
// 设置数据
NJContatc *c = self.contatcs[indexPath.row];//
cell.contatc = c;
// 2.返回cell
return cell;
} // 只在在tableview的编辑模式下才有添加 // 只要实现该方法, 手指在cell上面滑动的时候就自动实现了删除按钮
// commitEditingStyle: 传入提交的编辑操作(删除/添加)
// forRowAtIndexPath: 当前正在编辑的行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"%d", indexPath.row); if (UITableViewCellEditingStyleDelete == editingStyle) {
// 1.修改数据
[self.contatcs removeObjectAtIndex:indexPath.row];
// 2.刷新表格
// reloadData会重新调用数据的所有方法,刷新所有的行
// [self.tableView reloadData]; // 该方法用于删除tableview上指定行的cell
// 注意:使用该方法的时候,模型中删除的数据的条数必须和deleteRowsAtIndexPaths方法中删除的条数一致,否则会报错
// 简而言之,就删除的数据必须和删除的cell保持一致
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; // 3.更新保存的文件
[NSKeyedArchiver archiveRootObject:self.contatcs toFile:NJContactsPath]; }else if (UITableViewCellEditingStyleInsert == editingStyle)
{
// 添加一条数据
// NSLog(@"添加一条数据"); // 1.修改数据
NJContatc *c = [[NJContatc alloc] init];
c.name = @"xff";
c.phoneNumber = @""; // [self.contatcs addObject:c];
[self.contatcs insertObject:c atIndex:indexPath.row + ]; // NJContatc *c1 = [[NJContatc alloc] init];
// c1.name = @"xzz";
// c1.phoneNumber = @"123456";
// [self.contatcs insertObject:c1 atIndex:indexPath.row + 2]; // 2.刷新表格
// [self.tableView reloadData]; NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + inSection:];
// 注意点:数组中插入的条数必须和tableview界面上插入的cell条一致
// 否则程序会报错
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];
} } // 用于告诉系统开启的编辑模式是什么模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"indexPath = %d", indexPath.row);
// return UITableViewCellEditingStyleInsert; if (indexPath.row % == ) {
return UITableViewCellEditingStyleInsert;
}else
{
return UITableViewCellEditingStyleDelete;
}
} #pragma mark - 懒加载
- (NSMutableArray *)contatcs
{
if (_contatcs == nil) { // 1.获取路径
// NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"contacts.arc"];
// 2.从文件中读取数组
_contatcs = [NSKeyedUnarchiver unarchiveObjectWithFile:NJContactsPath]; // 3.如果第一次启动没有文件,就创建一个空的数组用于保存数据
if (_contatcs == nil) {
_contatcs = [NSMutableArray array];
} }
return _contatcs;
}

IOS tableView的数据刷新的更多相关文章

  1. IOS tableview下拉刷新上拉加载分页

    http://code4app.com/ios/快速集成下拉上拉刷新/52326ce26803fabc46000000 刷新没用用插件,加载使用的MJ老师的插件. - (void)viewDidLoa ...

  2. IOS开发UI基础--数据刷新

    IOS开发UI基础--数据刷新 cell的数据刷新包括下面几个方面 加入数据 删除数据 更改数据 全局刷新方法(最经常使用) [self.tableView reloadData]; // 屏幕上的全 ...

  3. 李洪强iOS开发之 - 指定刷新tableview的某一组

    李洪强iOS开发之 - 指定刷新tableview的某一组

  4. iOS开发系列--数据存取

    概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储到数据库.例如前面IOS开发系列-Objective-C之Foundation框架的文章中提到归档.plist文件存储, ...

  5. IOS 开发下拉刷新和上拉加载更多

    IOS 开发下拉刷新和上拉加载更多 简介 1.常用的下拉刷新的实现方式 (1)UIRefreshControl (2)EGOTTableViewrefresh (3)AH3DPullRefresh ( ...

  6. 【转】iOS开发系列--数据存取

    原文: http://www.cnblogs.com/kenshincui/p/4077833.html#SQLite 概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储 ...

  7. JAVAFX之tableview界面实时刷新导致的内存溢出(自己挖的坑,爬着也要出来啊0.0)

    这几天遇到了一个问题,不幸开发的一个cs架构的工具,客户端开启后,内存一直在缓慢增长最终导致进程卡死,花了4天时间,终于爬出来了... 客户端通过timer定时器每30秒查询一次数据库以及一些业务逻辑 ...

  8. iOS TableView多级列表

    代码地址如下:http://www.demodashi.com/demo/15006.html 效果预览 ### 一.需求 TableView多级列表:分级展开或合并,逐级获取并展示其子级数据,可以设 ...

  9. iOS-UI控件之UITableView(四)- cell数据刷新

    TableView- 数据刷新 数据刷新 添加数据 删除数据 更改数据 全局刷新方法(最常用) [self.tableView reloadData]; // 屏幕上的所有可视的cell都会刷新一遍 ...

随机推荐

  1. PS2018学习笔记(30-35节)

    30-35:万能的钢笔-制图抠图必学-part(1-6) # 本节知识点: 钢笔工具 贝塞尔曲线 绘图方式 光标状态认识 路径 形状 形状工具 矢量蒙版 # 本节段落表: 钢笔工具知识 直线绘制知识 ...

  2. [Windows]获取当前时间(年/月/日/时/分/秒)

    struct tm* GetCurTime(time_t inTime) { struct tm* curTime = localtime(&inTime); curTime->tm_y ...

  3. 使用imp命令和exp命令对oracle数据库进行导入导出操作

    命令说明 imp命令和exp命令需要在cmd命令窗口下执行,并且需要在系统环境变量中配置imp,exp命令所在目录 导出命令 --全库导出 exp system/systempassword@orcl ...

  4. 2017-11-8 NOIP模拟赛

    1.足球联赛 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...

  5. Hadoop 2.0完全分布式集群搭建方法(CentOS7+Hadoop 2.7.7)

    本文详细介绍搭建4个节点的完全分布式Hadoop集群的方法,Linux系统版本是CentOS 7,Hadoop版本是2.7.7,JDK版本是1.8. 一.准备环境 1. 在VMware worksta ...

  6. vue-cli3.0 使用图形化界面创建和管理项目

    1.打开终端输入vue ui vue ui 2.创建项目 3.选择一套预设,点击创建项目按钮 4.等待安装 5.安装完成后 6.可以添加插件 7.项目依赖管理 8.项目配置管理 9.项目任务管理 10 ...

  7. [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  8. HTTP的一些理解

    URI是Uniform Resource Identifier的缩写,统一资源标识符.URI用字符串标识某一互联网资源,而URL标识资源的地点(互联网上所处的位置).可见URL是URI的子集. 典型的 ...

  9. Vs 排除的文件➕ 添加回项目。。。。

    显示所有文件之后吧,就能看到排除掉的文件了.右键之前的文件,包括在项目中即可.

  10. poj2186-Popular Cows(强连通分支)

    有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表2欢迎1 ...