A.需求
1.以LOL英雄列表为蓝本,给其加上实时修改英雄名称的功能
2.使用UIAlertView
3.全局刷新reloadData
4.局部刷新
 
 
B.实现
1.使用UIAlertView
    // 弹窗
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改英雄名称" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
 
2.全局刷新reloadData
    // 刷新数据
    // 1.全局刷新
   [self.tableView reloadData];
 
3.局部刷新
    // 2.局部刷新
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
 
 
C.主要代码
 //
// Hero.h
// LOLHero
//
// Created by hellovoidworld on 14/12/1.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface Hero : NSObject @property(nonatomic, copy) NSString *icon;
@property(nonatomic, copy) NSString *intro;
@property(nonatomic, copy) NSString *name; - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
+ (instancetype) heroWithDictionary:(NSDictionary *) dictionary;
+ (instancetype) hero; @end
 
 //
// Hero.m
// LOLHero
//
// Created by hellovoidworld on 14/12/1.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "Hero.h" @implementation Hero - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
if (self = [super init]) {
// self.icon = dictionary[@"icon"];
// self.intro = dictionary[@"intro"];
// self.name = dictionary[@"name"];
// 代替上方代码, 使用KVC
[self setValuesForKeysWithDictionary:dictionary];
} return self;
} + (instancetype) heroWithDictionary:(NSDictionary *) dictionary {
return [[self alloc] initWithDictionary:dictionary];
} + (instancetype) hero {
return [self heroWithDictionary:nil];
} @end
 
 //
// ViewController.m
// LOLHero
//
// Created by hellovoidworld on 14/12/1.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "ViewController.h"
#import "Hero.h" // 遵守了UITableViewDelegate才能响应行点击事件, 遵守UIAlertViewDelegate处理弹窗的事件
@interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate> // UITableView
@property (weak, nonatomic) IBOutlet UITableView *tableView; // 所有hero资料
@property(nonatomic, strong) NSArray *heros; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 设置dataSource
self.tableView.dataSource = self; // 设置行高
self.tableView.rowHeight = ; // 设置delegate
self.tableView.delegate = self;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /** 隐藏状态栏 */
- (BOOL)prefersStatusBarHidden {
return YES;
} /** 延迟加载hero数据 */
- (NSArray *) heros {
if (nil == _heros) {
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]]; NSMutableArray *herosArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
Hero *hero = [Hero heroWithDictionary:dict];
[herosArray addObject:hero];
} _heros = herosArray;
} return _heros;
} #pragma mark - 列表方法 // section数, 默认是1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
} // 特定section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.heros.count;
} // 特定行的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Hero *hero = self.heros[indexPath.row]; // 使用static修饰局部变量,能保证只分配一次存储空间
static NSString *hereCellId = @"hero"; // 使用特定标识符, 从缓存池查看时候有适合的cell存在
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hereCellId]; if (nil == cell) {
// 创建的时候指定标识符
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:hereCellId];
} // 标题
cell.textLabel.text = hero.name; // 副标题
cell.detailTextLabel.text = hero.intro; // 图标
cell.imageView.image = [UIImage imageNamed:hero.icon]; return cell;
} #pragma mark - 响应事件 /** 点击选中某行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 取得model
Hero *hero = self.heros[indexPath.row]; // 弹窗
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改英雄名称" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; // 设置delegate,也可以在init方法中指定
alertView.delegate = self; // 设置弹窗样式
alertView.alertViewStyle = UIAlertViewStylePlainTextInput; // 设置弹窗输入框内容
[alertView textFieldAtIndex:].text = hero.name; // 保存index
alertView.tag = indexPath.row; // 显示弹窗
[alertView show];
} /** 处理弹窗点击“确定”按钮事件 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// 取消
if ( == buttonIndex) return; // 确定
// 保存新的名字到model
Hero *hero = self.heros[alertView.tag];
hero.name = [alertView textFieldAtIndex:].text; // 刷新数据
// 1.全局刷新
// [self.tableView reloadData]; // 2.局部刷新
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; } @end
 

[iOS基础控件 - 6.5] UITableView的数据刷新的更多相关文章

  1. [iOS基础控件 - 6.0] UITableView

    A.需要掌握的 1.基本属性和方法 设置UITableView的dataSource.delegate UITableView多组数据和单组数据的展示 UITableViewCell的常见属性 UIT ...

  2. [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)

    A.概述      在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能      1.按钮点击后,显示为“已下载”,并且不 ...

  3. [iOS基础控件 - 6.7.1] 微博展示 代码

      Controller: // // ViewController.m // Weibo // // Created by hellovoidworld on 14/12/4. // Copyrig ...

  4. [iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示

    A.实现思路 1.拖入UITableView 2.拖曳.连线UITableView控件 3.Controller遵守UITalbeViewDataSource协议 4.设置UITableView的da ...

  5. iOS 基础控件(下)

    上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...

  6. [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储

    A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改)   这个代码 ...

  7. [iOS基础控件 - 6.9] 聊天界面Demo

    A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...

  8. [iOS基础控件 - 7.0] UIWebView

    A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的   2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...

  9. [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo

    A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用   B.实现步骤 1.准备plist文件和国旗图片     2.创建模型 // // Flag.h // Co ...

随机推荐

  1. android 点击重新加载界面设计

    在项目中经常会遇到这样的场合,用户点击了一个界面后要提示等待加载,最后有可能显示加载失败,点击屏幕再重试加载.下面是该实例的代码: layout: loading.xml <?xml versi ...

  2. oracle 增加字段

    之前很多表增加很多相同的字段,一个一个添加太慢烦了,于是用了以下的方法alter table t_xmlc_batch_out_head_bak add ( SENDRECEIVEFLAG ) , S ...

  3. SQLite入门与分析(四)---Page Cache之事务处理(2)

    写在前面:个人认为pager层是SQLite实现最为核心的模块,它具有四大功能:I/O,页面缓存,并发控制和日志恢复.而这些功能不仅是上层Btree的基础,而且对系统的性能和健壮性有关至关重要的影响. ...

  4. Java IO5:序列化与反序列化

    一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存 ...

  5. 精选7款绚丽的HTML5和jQuery图片动画特效

    在HTML5出现后,图片就变得更加富有动感了,各种图片动画特效也层出不穷,例如图片播放器.图片导航.3D图片动画等等.本文将精选几款具有代表性的HTML5和jQuery图片动画特效,绚丽的画面.实用的 ...

  6. php cloure闭包

    Closures 它可以让您创建in-line 函数.许多语言已经开始有此功能了,也许您在不知道的情况下也使用过它. 例如: <?php $myFunction = function() { e ...

  7. CentOS 命令随笔

     linux下敲命令时:快速删除当前行已经敲的命令: CTR+U  或者 CTR+/                         快速删除当前行刚输入接近鼠标当前位置的单词:CTR+W 以上在XS ...

  8. Ubuntu12.04安装JDK6

    因为我们要搭建Android2.3的开发环境,只需要安装JDK6就可以,下面是其下载地址: http://www.oracle.com/technetwork/java/javase/download ...

  9. Java设计模式之工厂模式(简单工厂模式+工厂方法模式)

    摘自http://blog.csdn.net/jason0539/article/details/23020989 在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是 ...

  10. 普通方式 分页【NOT IN】和【>】效率大PK 千万级别数据测试结果

    首现创建一张表,然后插入1000+万条数据,接下来进行测试. use TTgoif exists (select * from sysobjects where name='Tonge')drop t ...