iOS开发UI篇—实现UItableview控件数据刷新

一、项目文件结构和plist文件

二、实现效果

1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作).

运行界面:

点击选中行:

修改数据后自动刷新:

三、代码示例

数据模型部分:

YYheros.h文件

 //
// YYheros.h
// 10-英雄展示(数据刷新)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Global.h" @interface YYheros : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *intro; //-(instancetype)initWithDict:(NSDictionary *)dict;
//+(instancetype)herosWithDict:(NSDictionary *)dict;
YYinitH(hero)
@end

YYheros.m文件

 //
// YYheros.m
// 10-英雄展示(数据刷新)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYheros.h" @implementation YYheros
//-(instancetype)initWithDict:(NSDictionary *)dict
//{
// if (self=[super init]) {
//// self.name=dict[@"name"];
//// self.icon=dict[@"icon"];
//// self.intro=dict[@"intro"];
//
// //使用KVC
// [self setValuesForKeysWithDictionary:dict];
// }
// return self;
//}
//
//+(instancetype)herosWithDict:(NSDictionary *)dict
//{
// return [[self alloc]initWithDict:dict];
//}
YYinitM(hero)
@end

主控制器 YYViewController.m文件

 //
// YYViewController.m
// 10-英雄展示(数据刷新)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYViewController.h"
#import "YYheros.h" @interface YYViewController ()<UITableViewDataSource,UIAlertViewDelegate,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property(nonatomic,strong)NSArray *heros;
@end @implementation YYViewController - (void)viewDidLoad
{
[super viewDidLoad];
//设置数据源
self.tableview.dataSource=self;
self.tableview.delegate=self;
self.tableview.rowHeight=.f;
NSLog(@"%d",self.heros.count);
} #pragma mark -懒加载
-(NSArray *)heros
{
if (_heros==nil) { NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath]; NSMutableArray *arrayM=[NSMutableArray array];
for (NSDictionary *dict in temparray) {
YYheros *hero=[YYheros herosWithDict:dict];
[arrayM addObject:hero];
}
_heros=[arrayM mutableCopy];
}
return _heros;
} #pragma mark- tableview的处理
//多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
//多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
//每组每行的数据,设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"cellForRowAtIndexPath 修改的了 %d", indexPath.row);
//1.去缓存中取
static NSString *identifier=@"hero";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//2.如果没有,那么就自己创建
if (cell==nil) {
NSLog(@"chuangjiancell");
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//3.设置数据 //3.1拿到该行的模型
YYheros *hero=self.heros[indexPath.row];
cell.textLabel.text=hero.name;
cell.imageView.image=[UIImage imageNamed:hero.icon];
cell.detailTextLabel.text=hero.intro;
//4.返回cell
return cell;
} #pragma mark-数据刷新
//1.弹出窗口,拿到数据
//当某一行被选中的时候调用该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//拿到改行的数据模型
YYheros *hero=self.heros[indexPath.row];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改数据" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; //密码框形式的
//alert.alertViewStyle=UIAlertViewStyleSecureTextInput;
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
UITextField *text=[alert textFieldAtIndex:];
//把当前行的英雄数据显示到文本框中
text.text=hero.name;
alert.tag=indexPath.row;
[alert show];
}
//2.修改数据,完成刷新操作
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//1.修改模型
//如果选中的是取消,那么就返回,不做任何操作
if (==buttonIndex) return;
//否则就修改模型,刷新数据
YYheros *hero=self.heros[alertView.tag]; //拿到当前弹窗中的文本数据(已经修改后的数据)
UITextField *text=[alertView textFieldAtIndex:];
//用修改后的数据去修改模型
hero.name=text.text; //2.刷新数据
// 只要调用tableview的该方法就会自动重新调用数据源的所有方法
// 会自动调用numberOfSectionsInTableView
// 会自动调用numberOfRowsInSection
// 会自动调用cellForRowAtIndexPath
// [self.tableview reloadData]; // 刷新指定行
NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:];
[self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
//如果不进行刷新会怎么样?(修改之后不会即时刷新,要等到重新对cell进行数据填充的时候才会刷新)
}
//隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end

四、把常用的代码封装成一个带参数的宏

封装方法和代码:

 //
// Global.h
// 10-英雄展示(数据刷新)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
// #ifndef _0____________Global_h
#define _0____________Global_h /**
* 自定义带参数的宏
*/
#define YYinitH(name) -(instancetype)initWithDict:(NSDictionary *)dict;\
+(instancetype)herosWithDict:(NSDictionary *)dict; #define YYinitM(name) -(instancetype)initWithDict:(NSDictionary *)dict\
{\
if (self=[super init]) {\
[self setValuesForKeysWithDictionary:dict];\
}\
return self;\
}\
\
+(instancetype)herosWithDict:(NSDictionary *)dict\
{\
return [[self alloc]initWithDict:dict];\
}\ #endif

以后在需要使用的时候,只需要使用宏即可。

如在YYheros.m文件中使用YYinitM(hero)这一句代码可以代替下面的代码段:

-(instancetype)initWithDict:(NSDictionary *)dict
{
if (self=[super init]) {
// self.name=dict[@"name"];
// self.icon=dict[@"icon"];
// self.intro=dict[@"intro"]; //使用KVC
[self setValuesForKeysWithDictionary:dict];
}
return self;
} +(instancetype)herosWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}

五、注意点

1.刷新数据的两个步骤:

1)修改模型

2)刷新表格数据(可以全部刷新,也可以刷新指定的行)

2.在主控制器文件中,遵守了三个协议

分别是:

UITableViewDataSource,

UIAlertViewDelegate,

UITableViewDelegate

iOS开发UI篇—实现UItableview控件数据刷新的更多相关文章

  1. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

    iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...

  2. iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

    iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...

  3. iOS开发UI篇—手写控件,frame,center和bounds属性

    iOS开发UI基础—手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...

  4. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)

    一.实现效果             二.实现代码 1.数据模型部分 YYQQGroupModel.h文件 // // YYQQGroupModel.h // 02-QQ好友列表(基本数据的加载) / ...

  5. iOS开发UI篇—在UItableview中实现加载更多功能

    一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据.                      二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...

  6. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  7. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...

  8. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

  9. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

随机推荐

  1. wampserver-----------如何设置wampserver在windows下开机自动启动。

    虽然很简单,但是还是做个记录.我的习惯,还是看图: 到你电脑的服务里面找到这两项然后点击右键属性,设置为自动.

  2. Oracle 正则表达式函数-REGEXP_REPLACE 使用例子

    原文在这: 戳 REGEXP_REPLACE 6个参数 第一个是输入的字符串 第二个是正则表达式 第三个是替换的字符 第四个是标识从第几个字符开始正则表达式匹配.(默认为1) 第五个是标识第几个匹配组 ...

  3. paper 107:图像的白平衡

    所谓白平衡(White Balance):指在图像处理的过程中,对原本材质为白色的物体的图像进行色彩还原,去除外部光源色温的影响,使其在照片上也显示白色.也就是不管在任何光源下,都能将白色物体还原为白 ...

  4. Scala正则和抽取器:解析方法参数

    在<正则表达式基础知识>中概括了正则表达式的基础知识, 本文讲解如何使用正则表达式解析方法参数,从而可以根据 DAO 自动生成 Service. 在做 Java 项目时,常常要根据 DAO ...

  5. Abot爬虫和visjs

    1. 引言 最近接触Abot爬虫也有几天时间了,闲来无事打算从IMDB网站上爬取一些电影数据玩玩.正好美国队长3正在热映,打算爬取漫威近几年的电影并用vis这个JS库呈现下漫威宇宙的相关电影. Abo ...

  6. HTML5 aria- and role

    HTML5 aria-* and role 在video-js的demo中看到了很多aria-*,不知道干嘛的.google一下,发现aria的意思是Accessible Rich Internet ...

  7. [Machine-Learning] 熟悉 Numpy

    Numpy 是 Python 中的一个模块,主要用于处理数学和计算相关的问题,这里是一个入门的介绍. 导入 习惯上可以这样导入: import numpy as np 在 machine learni ...

  8. SQL Server 2008教程和Microsoft® SQL Server® 2008 R2 SP2 - Express Edition下载

    教程 SQL Server 2008 Tutorialhttp://www.quackit.com/sql_server/sql_server_2008/tutorial/ 数据库下载 Microso ...

  9. 我的android学习经历33

    在Activity中添加菜单 1.在res目录下新建文件夹menu 右击res,选择new->Folder,Folder name写为menu 2.在新建的menu目录下新建一个xml文件 右击 ...

  10. Linux -- 文件统计常用命令

    标签(空格分隔): Linux sort -- 文件内排序命令 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次比较其ASCII码. 按每行升序排序: sort seq.tx ...