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. jmeter summariser(命令行执行时的输出) 、查看结果树等结果中文乱码

    在使用jmeter测试时,如果你的sampler名字为中文.或者输出的结果信息有中文,你会发现它们都是乱码,非常蛋碎!原因是:  jmeter的默认编码为:ISO-8859-1, 解决方案就是要修改它 ...

  2. jquery layout学习

    1.官网:http://layout.jquery-dev.com/index.cfm 2.博客园:http://www.cnblogs.com/chen-fan/articles/2044556.h ...

  3. ssh-keygen的使用方法

    一.概述 1.就是为了让两个linux机器之间使用ssh不需要用户名和密码.采用了数字签名RSA或者DSA来完成这个操作 2.模型分析 假设 A (192.168.20.59)为客户机器,B(192. ...

  4. 统计 p-value 含义

    p-value是一种概率:在原假设为真的前提下,出现该样本或比该样本更极端的结果的概率之和. 例子: 我们假设 H0:出现正面的概率是1/2 扔硬币20次出现了14次正面.该样本的单边p-value计 ...

  5. 254 shades of grey

    254 shades of grey Description: Why would we want to stop to only 50 shades of grey? Let's see to ho ...

  6. Windows使用virtualenv搭建flask开发环境

    virtualenv: VirtualEnv用于在一台机器上创建多个独立的Python虚拟运行环境,多个Python环境相互独立,互不影响,它能够: 在没有权限的情况下安装新套件 不同应用可以使用不同 ...

  7. java中的log中的用法和小结

    Log.logInfo(s.toString());的控制台显示 jog.info的具体用法. import java.io.*; import org.apache.log4j.Logger; im ...

  8. BZOJ3175: [Tjoi2013]攻击装置

    题解: 最大点独立集...好像水过头了... 不过发现我二分图好像忘完了!!! 代码: #include<cstdio> #include<cstdlib> #include& ...

  9. HHVM 是如何提升 PHP 性能的?

    背景 HHVM 是 Facebook 开发的高性能 PHP 虚拟机,宣称比官方的快9倍,我很好奇,于是抽空简单了解了一下,并整理出这篇文章,希望能回答清楚两方面的问题: HHVM 到底靠谱么?是否可以 ...

  10. CSS sprites 技术

    Css Sprites 技术逐渐流行,各大网站上都可以看到它的身影. 但从本质上,Css Sprites 只是 Css 技术的一个使用小窍门,初学者也能快速上手. Css Sprites 简单解释: ...