[iOS基础控件-6.5]UITableView的数据刷新

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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.主要代码

1//2//Hero.h3//LOLHero4//5//Created by hellovoidworld on 14/12/1.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import<Foundation/Foundation.h>1011@interfaceHero : NSObject1213@PRoperty(nonatomic, copy) NSString *icon;14@property(nonatomic, copy) NSString *intro;15@property(nonatomic, copy) NSString *name;1617- (instancetype) initWithDictionary:(NSDictionary *) dictionary;18+ (instancetype) heroWithDictionary:(NSDictionary *) dictionary;19+(instancetype) hero;2021@end

1//2//Hero.m3//LOLHero4//5//Created by hellovoidworld on 14/12/1.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"Hero.h"1011@implementationHero1213- (instancetype) initWithDictionary:(NSDictionary *) dictionary {14if(self =[super init]) {15//self.icon = dictionary[@"icon"];16//self.intro = dictionary[@"intro"];17//self.name = dictionary[@"name"];18//代替上方代码, 使用KVC19[self setValuesForKeysWithDictionary:dictionary];20}2122returnself;23}2425+ (instancetype) heroWithDictionary:(NSDictionary *) dictionary {26return[[self alloc] initWithDictionary:dictionary];27}2829+(instancetype) hero {30return[self heroWithDictionary:nil];31}3233@end

1//2//ViewController.m3//LOLHero4//5//Created by hellovoidworld on 14/12/1.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"ViewController.h"10#import"Hero.h"1112//遵守了UITableViewDelegate才能响应行点击事件, 遵守UIAlertViewDelegate处理弹窗的事件13@interfaceViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>1415//UITableView16@property (weak, nonatomic) IBOutlet UITableView *tableView;1718//所有hero资料19@property(nonatomic, strong) NSArray *heros;2021@end2223@implementationViewController2425- (void)viewDidLoad {26[super viewDidLoad];27//Do any additional setup after loading the view, typically from a nib.2829//设置dataSource30self.tableView.dataSource =self;3132//设置行高33self.tableView.rowHeight =60;3435//设置delegate36self.tableView.delegate=self;37}3839- (void)didReceiveMemoryWarning {40[super didReceiveMemoryWarning];41//Dispose of any resources that can be recreated.42}4344/** 隐藏状态栏*/45-(BOOL)prefersstatusBarHidden {46returnYES;47}4849/** 延迟加载hero数据*/50- (NSArray *) heros {51if(nil ==_heros) {52NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist"ofType:nil]];5354NSMutableArray *herosArray =[NSMutableArray array];55for(NSDictionary *dictindictArray) {56Hero *hero =[Hero heroWithDictionary:dict];57[herosArray addObject:hero];58}5960_heros =herosArray;61}6263return_heros;64}6566#pragmamark - 列表方法6768//section数, 默认是169- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {70return1;71}7273//特定section的行数74- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {75returnself.heros.count;76}777879//特定行的内容80- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {81Hero *hero =self.heros[indexPath.row];8283//使用static修饰局部变量,能保证只分配一次存储空间84staticNSString *hereCellId =@"hero";8586//使用特定标识符, 从缓存池查看时候有适合的cell存在87UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:hereCellId];8889if(nil ==cell) {90//创建的时候指定标识符91cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:hereCellId];92}9394//标题95cell.textLabel.text =hero.name;9697//副标题98cell.detailTextLabel.text =hero.intro;99100//图标101cell.imageView.image =[UIImage imageNamed:hero.icon];102103returncell;104}105106#pragmamark - 响应事件107108/** 点击选中某行*/109- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {110//取得model111Hero *hero =self.heros[indexPath.row];112113//弹窗114UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改英雄名称"message:nildelegate:nil cancelButtonTitle:@"取消"otherButtonTitles:@"确定", nil];115116//设置delegate,也可以在init方法中指定117alertView.delegate=self;118119//设置弹窗样式120alertView.alertViewStyle =UIAlertViewStylePlainTextInput;121122//设置弹窗输入框内容123[alertView textFieldAtIndex:0].text =hero.name;124125//保存index126alertView.tag =indexPath.row;127128//显示弹窗129[alertView show];130}131132/** 处理弹窗点击“确定”按钮事件*/133- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {134//取消135if(0== buttonIndex)return;136137//确定138//保存新的名字到model139Hero *hero =self.heros[alertView.tag];140hero.name = [alertView textFieldAtIndex:0].text;141142//刷新数据143//1.全局刷新144//[self.tableView reloadData];145146//2.局部刷新147NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];148[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];149150}151152@end

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航