IOS tableView的数据刷新
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的数据刷新的更多相关文章
- IOS tableview下拉刷新上拉加载分页
http://code4app.com/ios/快速集成下拉上拉刷新/52326ce26803fabc46000000 刷新没用用插件,加载使用的MJ老师的插件. - (void)viewDidLoa ...
- IOS开发UI基础--数据刷新
IOS开发UI基础--数据刷新 cell的数据刷新包括下面几个方面 加入数据 删除数据 更改数据 全局刷新方法(最经常使用) [self.tableView reloadData]; // 屏幕上的全 ...
- 李洪强iOS开发之 - 指定刷新tableview的某一组
李洪强iOS开发之 - 指定刷新tableview的某一组
- iOS开发系列--数据存取
概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储到数据库.例如前面IOS开发系列-Objective-C之Foundation框架的文章中提到归档.plist文件存储, ...
- IOS 开发下拉刷新和上拉加载更多
IOS 开发下拉刷新和上拉加载更多 简介 1.常用的下拉刷新的实现方式 (1)UIRefreshControl (2)EGOTTableViewrefresh (3)AH3DPullRefresh ( ...
- 【转】iOS开发系列--数据存取
原文: http://www.cnblogs.com/kenshincui/p/4077833.html#SQLite 概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储 ...
- JAVAFX之tableview界面实时刷新导致的内存溢出(自己挖的坑,爬着也要出来啊0.0)
这几天遇到了一个问题,不幸开发的一个cs架构的工具,客户端开启后,内存一直在缓慢增长最终导致进程卡死,花了4天时间,终于爬出来了... 客户端通过timer定时器每30秒查询一次数据库以及一些业务逻辑 ...
- iOS TableView多级列表
代码地址如下:http://www.demodashi.com/demo/15006.html 效果预览 ### 一.需求 TableView多级列表:分级展开或合并,逐级获取并展示其子级数据,可以设 ...
- iOS-UI控件之UITableView(四)- cell数据刷新
TableView- 数据刷新 数据刷新 添加数据 删除数据 更改数据 全局刷新方法(最常用) [self.tableView reloadData]; // 屏幕上的所有可视的cell都会刷新一遍 ...
随机推荐
- Android APK反编译技巧全讲解
导言:在我们安卓开发当中,我们不仅需要掌握基础的开发技能,也需要掌握软件的安全技能,这样才可以让我们的软件能够成为一款能够真正可以进行发布的软件,同时也可以让自己的核心技术不会被别人所盗取. 首先我们 ...
- cinder服务状态up/down的源码梳理
基于ocata版本的,源码梳理 1)用户输入cinder service-list命令行,查看cinder服务的状态时,cinder的入口函数为cinder/api/contrib/services. ...
- 置换群(本蒟蒻瞎BB的)(未完)
置换群(本蒟蒻瞎BB的)(未完) 群的定义 给定一个集合\(G=\{a, b, c...\}\)和集合\(G\)上的二元运算*,并满足: 封闭性:\(\forall a, b \in G, \exis ...
- Git Remote (转)
基本使用 git是一个分布式代码管理工具,所以可以支持多个仓库,在git里,服务器上的仓库在本地称之为remote. 直接clone一个仓库: $: git clone git@search.ued. ...
- Helvetic Coding Contest 2016 online mirror F1
Description Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure ...
- D. Time to Raid Cowavans 分块暴力,感觉关键在dp
http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...
- java里如何实现循环打印出字符或字符数组里的内容
不多说,直接上干货! java里如何实现循环打印出字符里的内容 没写,暂时不会 java里如何实现循环打印出字符数组里的内容 public class test { public static voi ...
- java使用线程请求访问每次间隔10分钟连续5次,之后停止请求
java使用线程请求访问每次间隔10分钟连续5次,收到相应的时候停止请求 package com.qlwb.business.util; /** * * * @类编号: * @类名称:RequestT ...
- synchronized重入后抛出异常,锁释放了吗
synchronized: 用于同步方法或者代码块,使得多个线程在试图并发执行同一个代码块的时候,串行地执行.以达到线程安全的目的. 允许重入: 在多线程的时候是这样的,但是对于单线程,是允许重入的, ...
- JavaScript初识(三)
十三丶JS中的面向对象 创建对象的几种常用方式: 1.使用Object或对象字面量创建对象 2.工厂模式创建对象 3.构造函数模式创建对象 4.原型模式创建对象 下面我们详细看一下如何创建对象 1.使 ...