数据刷新
1、UITableView通过代理实现监听。
*点击某行,弹出对话框,然后修改数据,再把数据刷新到UITableView上。
1>监听每个Cell的点击事件
*通过代理来监听。
**选中某行:-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath;
**取消选中某行:-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
注意:当点击切换行的时候会触发以上两个方法。
2>弹出UIAlertView
*修改弹出对话框的样式
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
*根据索引获取指定的某个文本框
[alertView textFieldAtIndex: 0];
[alertView textFieldAtIndex:0 ].text = hero.name;
*通过UIAlertView的代理来监听对话框中的按钮点击事件。
*实现UIAlertView的-(void)alertView:(UIAlertView *) alertView clickeduttonAtIndex:(NSInteger) buttonIndex; 代理方法。
2、具体步骤如下:
(1)让当前控制器遵守UITableViewDataSource协议和UItableViewDelegate协议,和UIAlertViewDelegate协议。
注意:因为要实现UITableView表格的显示必须要有数据源协议中的方法。因为要实现选中或取消单元格时的触发事件,所以必须要实现UITableView代理协议中的方法。 因为在弹出对话框后要对对话框内的组件进行操作。所以要遵守对话框的代理协议。
(2)实现协议中的方法。
(3)注意对话框中按钮的监听方法在对话框协议中定义的,有多个按钮时根据索引区别。
(4)对话框协议中的监听方法如下所示,要在对话框的监听方法内
对单元格内容进行修改的步骤如下:
1> 判断按钮的索引,到底是对话框上第几个按钮的监听
2> 获取用户文本框中输入的内容
3> 找到对应的英雄模型
4> 修改英雄模型的name
5>刷新tableView(重新刷新数据的意思就是重新调用UITableView的数据源中的那些数据源方法)
调用UItableView的reloadData方法即可刷新UITableView的数据。即此方法表示刷新整个UITableView。
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@“编辑英雄”message:@“哈哈” delegate: XXX cancelButtonTitle:@“取消” otherButtonTitles:@“确定”,nil];
// 修改对话框的格式,使其显示出一个文本框
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
// 获取那个文本框,并且设置文本框中的文字内容
[alertVIew textFieldAtIndex:0].text = hero.name
// 显示对话框
1、UITableView通过代理实现监听。
*点击某行,弹出对话框,然后修改数据,再把数据刷新到UITableView上。
1>监听每个Cell的点击事件
*通过代理来监听。
**选中某行:-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath;
**取消选中某行:-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
注意:当点击切换行的时候会触发以上两个方法。
2>弹出UIAlertView
*修改弹出对话框的样式
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
*根据索引获取指定的某个文本框
[alertView textFieldAtIndex: 0];
[alertView textFieldAtIndex:0 ].text = hero.name;
*通过UIAlertView的代理来监听对话框中的按钮点击事件。
*实现UIAlertView的-(void)alertView:(UIAlertView *) alertView clickeduttonAtIndex:(NSInteger) buttonIndex; 代理方法。
2、具体步骤如下:
(1)让当前控制器遵守UITableViewDataSource协议和UItableViewDelegate协议,和UIAlertViewDelegate协议。
注意:因为要实现UITableView表格的显示必须要有数据源协议中的方法。因为要实现选中或取消单元格时的触发事件,所以必须要实现UITableView代理协议中的方法。 因为在弹出对话框后要对对话框内的组件进行操作。所以要遵守对话框的代理协议。
(2)实现协议中的方法。
(3)注意对话框中按钮的监听方法在对话框协议中定义的,有多个按钮时根据索引区别。
(4)对话框协议中的监听方法如下所示,要在对话框的监听方法内
对单元格内容进行修改的步骤如下:
1> 判断按钮的索引,到底是对话框上第几个按钮的监听
2> 获取用户文本框中输入的内容
3> 找到对应的英雄模型
4> 修改英雄模型的name
5>刷新tableView(重新刷新数据的意思就是重新调用UITableView的数据源中的那些数据源方法)
调用UItableView的reloadData方法即可刷新UITableView的数据。即此方法表示刷新整个UITableView。
而ReloadRowsAtIndexPaths表示刷新指定的行,即局部刷新。
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@“编辑英雄”message:@“哈哈” delegate: XXX cancelButtonTitle:@“取消” otherButtonTitles:@“确定”,nil];
// 修改对话框的格式,使其显示出一个文本框
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
// 获取那个文本框,并且设置文本框中的文字内容
[alertVIew textFieldAtIndex:0].text = hero.name
// 显示对话框
[alertView show];
在LOL英雄展示的基础之上进行修改
代码如下plist文件如下:
文件布局如下:
编辑Hero.h如下:
//
// Hero.h
// LOL英雄榜UITableView加载plist展示单组数据
//
// Created by apple on 15/8/31.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Hero : NSObject
@property (nonatomic, strong) NSString *icon;
@property (nonatomic, strong) NSString *intro;
@property (nonatomic, strong) NSString *name;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)heroWithDict:(NSDictionary *)dict;
@end
编辑Hero.m如下:
//
// Hero.m
// LOL英雄榜UITableView加载plist展示单组数据
//
// Created by apple on 15/8/31.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "Hero.h"
@implementation Hero
-(instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
// self.name = dict[@"name"];
// self.icon = dict[@"icon"];
// self.intro = dict[@"intro"];
[self setValuesForKeysWithDictionary:dict]; // KVC
}
return self;
}
+(instancetype)heroWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
编辑控制器的.h文件如下:
//
// ViewController.h
// LOL英雄榜UITableView加载plist展示单组数据
//
// Created by apple on 15/8/31.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *array;
@end
编辑控制器的.m文件如下:
//
// ViewController.m
// LOL英雄榜UITableView加载plist展示单组数据
//
// Created by apple on 15/8/31.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
#import "Hero.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 添加UITableView控件
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
self.tableView.dataSource = self; // 设置此UITableView组件的数据源对象为当前控制器
self.tableView.delegate = self; // 设置此UITableView组件的代理对象为当前控制器
self.tableView.showsVerticalScrollIndicator = NO; // 取消竖直滑动条
self.tableView.rowHeight = 70; // 只对于所有行高度都相同的情况下
[self.view addSubview:self.tableView];
}
// 重写数组的get方法实现懒加载
-(NSArray *)array
{
if (_array == nil) {
//查找plist文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
// 根据路径读取plist文件到一个数组
NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
// 新建空的一个专门存放model对象的可变数组
NSMutableArray *arrayModel = [NSMutableArray array];
// 遍历从文件读取存放所有字典的数组,逐个转成model对象存放到可变数组中
for (NSDictionary * dict in arrayDict){
// 新建一个model对象
Hero *hero = [Hero heroWithDict:dict];
[arrayModel addObject:hero];
}
_array = arrayModel;
}
return _array;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 取消状态栏
-(BOOL)prefersStatusBarHidden
{
return YES;
}
// 利用UITableViewDelegate代理协议中的方法,即使行高不相同的也行
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row %2 == 0) {
return 60;
}else{
return 80;
}
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 新建指定格式的单元格对象
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
// 获取model数据
Hero *hero = self.array[indexPath.row];
cell.imageView.image = [UIImage imageNamed:hero.icon];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
// 添加箭头
/* accessoryType 的枚举值如下:
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks
UITableViewCellAccessoryCheckmark, // checkmark. doesn't track
UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks*/
if(indexPath.row <1){
cell.accessoryType = UITableViewCellAccessoryNone; // 默认状态下不显示任何组件
}else if(indexPath.row<2){
cell.accessoryType = UITableViewCellAccessoryDetailButton ; //显示详细信息按钮
}else if (indexPath.row<3){
cell.accessoryType = UITableViewCellAccessoryCheckmark; // 添加选择按钮
}else if (indexPath.row <4){
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 添加箭头按钮
}else if (indexPath .row<5){
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; // 同时添加箭头与详细信息的按钮
}else if (indexPath.row <6 ){
cell.accessoryView = [[UISwitch alloc] init];
}else
cell.accessoryView = [[UIButton alloc] init];
return cell;
}
// 当点击了alertView上的按钮的时候会执行这个方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%d",buttonIndex);
if (buttonIndex == 1) {
// 表示点击的是“确定”
// 更新数据
// 1、获取用户文本框中输入的内容
NSString *name = [alertView textFieldAtIndex:0].text;
// 2、找到对应的英雄模型
Hero *hero = self.array[alertView.tag]; // 注意此时的hero是个对象指针指向预先数组项的位置
// 并不是新生成了另外一个hero
//3、修改英雄模型的name
hero.name = name;
//4、刷新tableView(重新刷新数据的意思就是重新调用UItableView的数据源中的那些数据方法)
// reloadData表示刷新的是整个tableView
// [self.tableView reloadData];
// 局部刷新,刷新指定的行
// 创建一个行对象 然后保存到数组内才能作为局部刷新方法的第一个参数
NSIndexPath *idxPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0]; // 因为UItableView在当前只有一个组,所以是0
[self.tableView reloadRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationLeft]; // 以向左滑动的动画形式进行展示
}
}
// 当选中某个单元格时触发UItableViewDelegate协议中的监听方法如下所示:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"+++++");
// 获取当前被选中的这行的英雄的名称
Hero *hero = self.array[indexPath.row];
// 创建一个提示框对象
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"编辑英雄" message:@"输入英雄名称" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
// 设置对话框格式,使其显示一个文本输入框
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
// 把当前行的索引保存到alertView的Tag中
alertView.tag = indexPath.row;
// 获取那个文本框,并且设置文本框的文字为hero.name
[alertView textFieldAtIndex:0].text = hero.name;
// 显示对话框
[alertView show];
}
@end
运行结果如下: