iOS開發UI篇—實現UItableview控制項資料重新整理

來源:互聯網
上載者:User

標籤:

iOS開發UI篇—實現UItableview控制項資料重新整理

一、專案檔結構和plist檔案

二、實現效果

1.說明:這是一個英雄展示介面,點擊選中行,可以修改改行英雄的名稱(完成資料重新整理的操作).

運行介面:

點擊選中行:

修改資料後自動重新整理:

三、程式碼範例

資料模型部分:

YYheros.h檔案

 1 // 2 //  YYheros.h 3 //  10-英雄展示(資料重新整理) 4 // 5 //  Created by apple on 14-5-29. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 #import "Global.h"11 12 @interface YYheros : NSObject13 @property(nonatomic,copy)NSString *name;14 @property(nonatomic,copy)NSString *icon;15 @property(nonatomic,copy)NSString *intro;16 17 //-(instancetype)initWithDict:(NSDictionary *)dict;18 //+(instancetype)herosWithDict:(NSDictionary *)dict;19 YYinitH(hero)20 @end

 YYheros.m檔案

 1 // 2 //  YYheros.m 3 //  10-英雄展示(資料重新整理) 4 // 5 //  Created by apple on 14-5-29. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYheros.h"10 11 @implementation YYheros12 //-(instancetype)initWithDict:(NSDictionary *)dict13 //{14 //    if (self=[super init]) {15 ////        self.name=dict[@"name"];16 ////        self.icon=dict[@"icon"];17 ////        self.intro=dict[@"intro"];18 //        19 //        //使用KVC20 //        [self setValuesForKeysWithDictionary:dict];21 //    }22 //    return self;23 //}24 //25 //+(instancetype)herosWithDict:(NSDictionary *)dict26 //{27 //    return [[self alloc]initWithDict:dict];28 //}29 YYinitM(hero)30 @end

主控制器 YYViewController.m檔案

  1 //  2 //  YYViewController.m  3 //  10-英雄展示(資料重新整理)  4 //  5 //  Created by apple on 14-5-29.  6 //  Copyright (c) 2014年 itcase. All rights reserved.  7 //  8   9 #import "YYViewController.h" 10 #import "YYheros.h" 11  12 @interface YYViewController ()<UITableViewDataSource,UIAlertViewDelegate,UITableViewDelegate> 13 @property (strong, nonatomic) IBOutlet UITableView *tableview; 14 @property(nonatomic,strong)NSArray *heros; 15 @end 16  17 @implementation YYViewController 18  19 - (void)viewDidLoad 20 { 21     [super viewDidLoad]; 22     //設定資料來源 23     self.tableview.dataSource=self; 24     self.tableview.delegate=self; 25     self.tableview.rowHeight=60.f; 26     NSLog(@"%d",self.heros.count); 27 } 28  29 #pragma mark -懶載入 30 -(NSArray *)heros 31 { 32     if (_heros==nil) { 33          34         NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil]; 35         NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath]; 36          37         NSMutableArray *arrayM=[NSMutableArray array]; 38         for (NSDictionary *dict in temparray) { 39             YYheros *hero=[YYheros herosWithDict:dict]; 40             [arrayM addObject:hero]; 41         } 42         _heros=[arrayM mutableCopy]; 43     } 44     return _heros; 45 } 46  47 #pragma mark- tableview的處理 48 //多少組 49 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 50 { 51     return 1; 52 } 53 //多少行 54 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 55 { 56     return self.heros.count; 57 } 58 //每組每行的資料,設定cell 59 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 60 { 61     //NSLog(@"cellForRowAtIndexPath 修改的了 %d", indexPath.row); 62     //1.去緩衝中取 63     static NSString *[email protected]"hero"; 64     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 65     //2.如果沒有,那麼就自己建立 66     if (cell==nil) { 67         NSLog(@"chuangjiancell"); 68         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 69     } 70     //3.設定資料 71      72     //3.1拿到該行的模型 73     YYheros *hero=self.heros[indexPath.row]; 74     cell.textLabel.text=hero.name; 75     cell.imageView.image=[UIImage imageNamed:hero.icon]; 76     cell.detailTextLabel.text=hero.intro; 77     //4.返回cell 78     return cell; 79 } 80  81 #pragma mark-資料重新整理 82 //1.快顯視窗,拿到資料 83 //當某一行被選中的時候調用該方法 84 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 85 { 86     //拿到改行的資料模型 87     YYheros *hero=self.heros[indexPath.row]; 88     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改資料" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; 89      90     //密碼框形式的 91     //alert.alertViewStyle=UIAlertViewStyleSecureTextInput; 92     alert.alertViewStyle=UIAlertViewStylePlainTextInput; 93     UITextField *text=[alert textFieldAtIndex:0]; 94     //把當前行的英雄資料顯示到文字框中 95     text.text=hero.name; 96     alert.tag=indexPath.row; 97     [alert show]; 98 } 99 //2.修改資料,完成重新整理操作100 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex101 {102     //1.修改模型103     //如果選中的是取消,那麼就返回,不做任何操作104     if (0==buttonIndex) return;105     //否則就修改模型,重新整理資料106     YYheros *hero=self.heros[alertView.tag];107     108     //拿到當前彈窗中的文本資料(已經修改後的資料)109     UITextField *text=[alertView textFieldAtIndex:0];110     //用修改後的資料去修改模型111     hero.name=text.text;112    113     //2.重新整理資料114     // 只要調用tableview的該方法就會自動重新調用資料來源的所有方法115     // 會自動調用numberOfSectionsInTableView116     // 會自動調用numberOfRowsInSection117     // 會自動調用cellForRowAtIndexPath118     //    [self.tableview reloadData];119     120     // 重新整理指定行121        NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];122         [self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];123     //如果不進行重新整理會怎麼樣?(修改之後不會即時重新整理,要等到重新對cell進行資料填充的時候才會重新整理)124 }125 //隱藏狀態列126 -(BOOL)prefersStatusBarHidden127 {128     return YES;129 }130 @end

四、把常用的代碼封裝成一個帶參數的宏

封裝方法和代碼:

 1 // 2 //  Global.h 3 //  10-英雄展示(資料重新整理) 4 // 5 //  Created by apple on 14-5-29. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #ifndef _0____________Global_h10 #define _0____________Global_h11 12 /**13  *  自訂帶參數的宏14  */15 #define     YYinitH(name)   -(instancetype)initWithDict:(NSDictionary *)dict;16 +(instancetype)herosWithDict:(NSDictionary *)dict;17 18 19 #define     YYinitM(name)  -(instancetype)initWithDict:(NSDictionary *)dict20 {21     if (self=[super init]) {22         [self setValuesForKeysWithDictionary:dict];23     }24     return self;25 }26 27 +(instancetype)herosWithDict:(NSDictionary *)dict28 {29     return [[self alloc]initWithDict:dict];30 }31 32 #endif

以後在需要使用的時候,只需要使用宏即可。

如在YYheros.m檔案中使用YYinitM(hero)這一句代碼可以代替下面的程式碼片段:

-(instancetype)initWithDict:(NSDictionary *)dict{    if (self=[super init]) {//        self.name=dict[@"name"];//        self.icon=dict[@"icon"];//        self.intro=dict[@"intro"];                //使用KVC        [self setValuesForKeysWithDictionary:dict];    }    return self;}+(instancetype)herosWithDict:(NSDictionary *)dict{    return [[self alloc]initWithDict:dict];}

五、注意點

1.重新整理資料的兩個步驟:

1)修改模型

2)重新整理表格式資料(可以全部重新整理,也可以重新整理指定的行)

2.在主控制器檔案中,遵守了三個協議

分別是:

UITableViewDataSource,

UIAlertViewDelegate,

UITableViewDelegate

 

iOS開發UI篇—實現UItableview控制項資料重新整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.