国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[iOS基礎(chǔ)控件-6.5]UITableView的數(shù)據(jù)刷新

2019-11-14 19:48:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
A.需求
1.以L(fǎng)OL英雄列表為藍(lán)本,給其加上實(shí)時(shí)修改英雄名稱(chēng)的功能
2.使用UIAlertView
3.全局刷新reloadData
4.局部刷新
Image(79)Image(80)
 
 
B.實(shí)現(xiàn)
1.使用UIAlertView
    // 彈窗
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改英雄名稱(chēng)" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
 
2.全局刷新reloadData
    // 刷新數(shù)據(jù)
    // 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.h 3 //  LOLHero 4 // 5 //  Created by hellovoidworld on 14/12/1. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 @interface Hero : NSObject12 13 @PRoperty(nonatomic, copy) NSString *icon;14 @property(nonatomic, copy) NSString *intro;15 @property(nonatomic, copy) NSString *name;16 17 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;18 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary;19 + (instancetype) hero;20 21 @end
 
 1 // 2 //  Hero.m 3 //  LOLHero 4 // 5 //  Created by hellovoidworld on 14/12/1. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import "Hero.h"10 11 @implementation Hero12 13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {14     if (self = [super init]) {15 //        self.icon = dictionary[@"icon"];16 //        self.intro = dictionary[@"intro"];17 //        self.name = dictionary[@"name"];18         // 代替上方代碼, 使用KVC19         [self setValuesForKeysWithDictionary:dictionary];20     }21    22     return self;23 }24 25 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary {26     return [[self alloc] initWithDictionary:dictionary];27 }28 29 + (instancetype) hero {30     return [self heroWithDictionary:nil];31 }32 33 @end
 
  1 //  2 //  ViewController.m  3 //  LOLHero  4 //  5 //  Created by hellovoidworld on 14/12/1.  6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.  7 //  8   9 #import "ViewController.h" 10 #import "Hero.h" 11  12 // 遵守了UITableViewDelegate才能響應(yīng)行點(diǎn)擊事件, 遵守UIAlertViewDelegate處理彈窗的事件 13 @interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate> 14  15 // UITableView 16 @property (weak, nonatomic) IBOutlet UITableView *tableView; 17  18 // 所有hero資料 19 @property(nonatomic, strong) NSArray *heros; 20  21 @end 22  23 @implementation ViewController 24  25 - (void)viewDidLoad { 26     [super viewDidLoad]; 27     // Do any additional setup after loading the view, typically from a nib. 28     29     // 設(shè)置dataSource 30     self.tableView.dataSource = self; 31     32     // 設(shè)置行高 33     self.tableView.rowHeight = 60; 34     35     // 設(shè)置delegate 36     self.tableView.delegate = self; 37 } 38  39 - (void)didReceiveMemoryWarning { 40     [super didReceiveMemoryWarning]; 41     // Dispose of any resources that can be recreated. 42 } 43  44 /** 隱藏狀態(tài)欄 */ 45 - (BOOL)prefersstatusBarHidden { 46     return YES; 47 } 48  49 /** 延遲加載hero數(shù)據(jù) */ 50 - (NSArray *) heros { 51     if (nil == _heros) { 52         NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]]; 53         54         NSMutableArray *herosArray = [NSMutableArray array]; 55         for (NSDictionary *dict in dictArray) { 56             Hero *hero = [Hero heroWithDictionary:dict]; 57             [herosArray addObject:hero]; 58         } 59         60         _heros = herosArray; 61     } 62     63     return _heros; 64 } 65  66 #pragma mark - 列表方法 67  68 // section數(shù), 默認(rèn)是1 69 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 70     return 1; 71 } 72  73 // 特定section的行數(shù) 74 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 75     return self.heros.count; 76 } 77  78  79 // 特定行的內(nèi)容 80 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 81     Hero *hero = self.heros[indexPath.row]; 82     83     // 使用static修飾局部變量,能保證只分配一次存儲(chǔ)空間 84     static NSString *hereCellId = @"hero"; 85     86     // 使用特定標(biāo)識(shí)符, 從緩存池查看時(shí)候有適合的cell存在 87     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hereCellId]; 88     89     if (nil == cell) { 90         // 創(chuàng)建的時(shí)候指定標(biāo)識(shí)符 91         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:hereCellId]; 92     } 93  94     // 標(biāo)題 95     cell.textLabel.text = hero.name; 96     97     // 副標(biāo)題 98     cell.detailTextLabel.text = hero.intro; 99    100     // 圖標(biāo)101     cell.imageView.image = [UIImage imageNamed:hero.icon];102 103     return cell;104 }105 106 #pragma mark - 響應(yīng)事件107 108 /** 點(diǎn)擊選中某行 */109 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {110     // 取得model111     Hero *hero = self.heros[indexPath.row];112    113     // 彈窗114     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改英雄名稱(chēng)" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];115    116     // 設(shè)置delegate,也可以在init方法中指定117     alertView.delegate = self;118    119     // 設(shè)置彈窗樣式120     alertView.alertViewStyle = UIAlertViewStylePlainTextInput;121    122     // 設(shè)置彈窗輸入框內(nèi)容123     [alertView textFieldAtIndex:0].text = hero.name;124    125     // 保存index126     alertView.tag = indexPath.row;127    128     // 顯示彈窗129     [alertView show];130 }131 132 /** 處理彈窗點(diǎn)擊“確定”按鈕事件 */133 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {134     // 取消135     if (0 == buttonIndex) return;136    137     // 確定138     // 保存新的名字到model139     Hero *hero = self.heros[alertView.tag];140     hero.name = [alertView textFieldAtIndex:0].text;141    142     // 刷新數(shù)據(jù)143     // 1.全局刷新144 //    [self.tableView reloadData];145    146     // 2.局部刷新147     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];148     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];149    150 }151 152 @end

 

 

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三都| 德兴市| 康乐县| 固始县| 辛集市| 湘阴县| 兴和县| 大宁县| 明水县| 晋江市| 鸡西市| 西乡县| 明溪县| 泽库县| 池州市| 兴义市| 正蓝旗| 乌拉特中旗| 永城市| 卢湾区| 延寿县| 保山市| 定兴县| 侯马市| 沁源县| 临漳县| 佛坪县| 青州市| 张家口市| 杂多县| 宁津县| 普陀区| 蕉岭县| 明溪县| 揭东县| 河池市| 汝阳县| 息烽县| 枣庄市| 揭东县| 怀柔区|