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. C++基础之继承类和派生类

    (1)继承是创建一个具有某个类的属性和行为的新类的能力.原有的类称为基类,新创建的类称为派生类.派生类将基类中的所有成员作为自己的成员,同时派生类本身可以定义新的成员(2)派生类只有一个基类的继承称单 ...

  2. JavaScript -- 实现密码加密的几种方案

    base64加密 页面中引入base64.js var base=new Base64(); var str=base.encode('admin:admin'); //解密用: str=b.deco ...

  3. Yahoo Progamming Contest 2019D(DP,思维)

    #include<bits/stdc++.h>using namespace std;long long n,v,a,b,c,d,e;int main(){    scanf(" ...

  4. explain SQL语句()

    坊间有传言:MySQL性能优化有个神器,叫做explain,它可以对select语句进行分析并且输出详细的select执行过程的详细信息,让开发者从这些信息中获得优化的思路. 下面来讲讲这个MySQL ...

  5. VMware Workstation 安装以及Linux虚拟机安装 指北

    最近有挺多小伙伴跟我说起虚拟机这个东西,所以,今天就给大家写一篇虚拟机安装使用指北吧. 虚拟机(英语:virtual machine),在计算机科学中的体系结构里,是指一种特殊的软件,可以在计算机平台 ...

  6. express知识点

    本篇文章主要内容 1.用Express在系统文件夹内搭建一个服务器 2.Express的路由(来自 Express 文档) 3.Express的中间件(这才是关键) 4.Express的一些零碎的知识 ...

  7. [Leetcode]012. Integer to Roman

    public class Solution { public String intToRoman(int num) { String M[] = {"", "M" ...

  8. Luogu P4159 [SCOI2009]迷路 矩阵快速幂+精巧转化

    大致就是矩阵快速幂吧.. 这个时候会发现这些边权$\le 9$,然后瞬间想到上回一道题:是不是可以建一堆转移矩阵再建一个$lcm(1,2,3,4,5,6,7,8,9)$的矩阵?...后来发现十分的慢q ...

  9. Python----Anaconda + PyCharm + Python 开发环境搭建(使用pip,安装selenium,使用IDLE)

    1.Python开发中会用到的工具下载地址 FireBug插件安装地址:https://addons.mozilla.org/en-US/firefox/addon/firebug/ FirePath ...

  10. Windows下搭建QT环境

    必须软件 qt-windows-opensource-5.1.1-msvc2010-x86-offline qt-vs-addin-1.2.2-opensource支持vs2008.2010.2012 ...