iOS development UI Chapter-Implementing UITableView Control Data Refresh

Source: Internet
Author: User

iOS development UI Chapter-Implementing UITableView Control Data Refresh

I. Project document structure and plist file

Second, the realization effect

1. Description: This is a hero display interface, click on the selected line, you can modify the name of the row hero (the operation to complete the data refresh).

Operating interface:

Click on the selected line:

Automatic refresh after modifying data:

Third, code example

Data Model section:

YYheros.h file

1//2//  YYheros.h 3//  10-Hero display (data refresh) 4//5//  Created by Apple on 14-5-29.6//  Copyright (c) 2014 ITC Ase. All rights reserved. 7//8  9 #import <foundation/foundation.h>10 #import "Global.h" @interface yyheros:nsobject13 @property (n onatomic,copy) nsstring *name;14 @property (nonatomic,copy) nsstring *icon;15 @property (nonatomic,copy) NSString *intro ;//-(Instancetype) initwithdict: (nsdictionary *) dict;18//+ (instancetype) heroswithdict: (Nsdictionary *) dict;19 Yyinith (Hero) @end

YYHEROS.M file

1//2//  YYHEROS.M 3//  10-Hero display (data refresh) 4//5//  Created by Apple on 14-5-29.6//  Copyright (c) 2014 ITC Ase. All rights reserved. 7//8  9 #import "YYheros.h" @implementation YYheros12//-(instancetype) initwithdict: (nsdictionary *) dict13//{ +/    if (self=[super init]) {////        self.name=dict[@ "name"];16////self.icon=dict[@        "icon"];17////        self.intro=dict[@ "Intro"];18//+////        use KVC20//        [self setvaluesforkeyswithdictionary:dict]; +//    }22//    return self;23//}24//25//+ (instancetype) heroswithdict: (nsdictionary *) dict26//{27//    return [self alloc]initwithdict:dict];28//}29 yyinitm (Hero) @end

Host controller YYVIEWCONTROLLER.M file

  1//2//YYVIEWCONTROLLER.M 3//10-Hero Display (data refresh) 4//5//Created by Apple on 14-5-29. 6//Copyright (c) 2014 itcase.  All rights reserved. 7//8 9 #import "YYViewController.h" #import "YYheros.h" @interface Yyviewcontroller () <uitableviewdataso Urce,uialertviewdelegate,uitableviewdelegate> @property (Strong, nonatomic) Iboutlet UITableView *tableview; @property (Nonatomic,strong) Nsarray *heros;  @end @implementation Yyviewcontroller-(void) viewdidload {[Super viewdidload]; 22//Set Data source self.tableview.datasource=self; self.tableview.delegate=self; SELF.TABLEVIEW.ROWHEIGHT=60.F; NSLog (@ "%d", self.heros.count); #pragma mark-Lazy load-(Nsarray *) heros {+ if (_heros==nil) {NSString *fullpat H=[[nsbundle mainbundle]pathforresource:@ "Heros.plist" oftype:nil]; Nsarray *temparray=[nsarray Arraywithcontentsoffile:fullpath]; Approx. NSMutablearray *arraym=[nsmutablearray Array];             Nsdictionary *dict in Temparray) {Yyheros *hero=[yyheros heroswithdict:dict]; 40 [Arraym Addobject:hero]; _heros=[arraym Mutablecopy]; The _heros return; The processing of the #pragma mark-tableview 48//How many groups (Nsinteger) Numberofsectionsintableview: (UITableView *) TableView 50 {5 1 return 1; 52} 53//How many lines-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section, retur n Self.heros.count; 57} 58/per set of data per row, set cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath * ) Indexpath {//nslog (@ "Cellforrowatindexpath modified%d", indexpath.row); 62//1. Go to the cache and take the static NSString * [email protected] "hero"; UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:identifier]; 65//2. If not, create your own if (Cell==nil) {NSLog (@ "Chuangjiancell"); Cell=[[uitableviewcell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifier]; 69} 70//3. Set Data 71 72//3.1 Get the line model Yyheros *hero=self.heros[indexpath.row]; Cell.textlabel.text=hero.name; Cell.imageview.image=[uiimage ImageNamed:hero.icon]; Cell.detailtextlabel.text=hero.intro; 77//4. Return cell with return cell; The #pragma mark-data refresh 82//1. Pop-up window, get data 83//Call this method when a row is selected (void) TableView: (UITableView *) TableView Didselect Rowatindexpath: (Nsindexpath *) Indexpath 85 {86//Get the data model of the diverted Yyheros *hero=self.heros[indexpath.row]; UIA Lertview *alert=[[uialertview alloc]initwithtitle:@ "Modify data" Message:nil delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil]; 89 90//Password box in the form of the//alert.alertviewstyle=uialertviewstylesecuretextinput; Alert.alertviewstyle=uialertviewstyleplaintextinput; Uitextfield *text=[alert textfieldatindex:0]; 94//Show the hero data of the current line to the textIn this box, Text.text=hero.name; Alert.tag=indexpath.row; [Alert show]; 98} 99//2. Modify the data to complete the refresh operation (void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) buttonIndex101 { 102//1. Modify Model 103//If Cancel is selected, then return, do nothing 104 if (0==buttonindex) return;105//Otherwise modify the model, refresh the data 106 Yyheros *h ERO=SELF.HEROS[ALERTVIEW.TAG];107 108//Get the text data (modified data) in the current pop-up window 109 Uitextfield *text=[alertview Textfieldatinde X:0];110//Use modified data to modify the Model 111 hero.name=text.text;112 113//2. Refresh Data 114//The method that calls TableView will automatically recall all methods of the data source     115//will automatically call numberOfSectionsInTableView116//will automatically call numberOfRowsInSection117//will automatically call cellForRowAtIndexPath118 [Self.tableview reloaddata];119 120//Refresh specified line 121 nsindexpath *path = [Nsindexpath indexpathforrow:a Lertview.tag insection:0];122 [Self.tableview Reloadrowsatindexpaths:@[path] Withrowanimation:uitableviewrowanimat ionright];123//What happens if I don't refresh? (It will not be refreshed immediately after the change, it will not be brushed until the cell is re-populated.)New) 124}125//Hide Status bar 126-(BOOL) prefersStatusBarHidden127 {yes;129}130 @end 

Iv. encapsulating common code into a macro with parameters

Encapsulation Methods and code:

1//2//  Global.h 3//  10-Hero display (data refresh) 4//5//  Created by Apple on 14-5-29.6//  Copyright (c) 2014 ITCA Se. All rights reserved. 7//8  9 #ifndef _0____________global_h10 #define _0____________GLOBAL_H11/**13  *  custom macros with parameters  #define     Yyinith (name)   -(Instancetype) Initwithdict: (Nsdictionary *) dict;16 + (instancetype) Heroswithdict: (nsdictionary *) dict;17 #define     yyinitm (name)  -(Instancetype) Initwithdict: ( Nsdictionary *) dict20 {     if (self=[super init]) {[Self         setvaluesforkeyswithdictionary:dict];23     }     return self;25}26 + (instancetype) heroswithdict: (Nsdictionary *) dict28 {     return [[Self alloc] Initwithdict:dict];30}31 #endif

In the future, when you need to use it, you only need to use a macro.

If you use YYINITM (Hero) in a yyheros.m file, this code can replace the following code snippet:

-(Instancetype) Initwithdict: (nsdictionary *) dict{    if (self=[super init]) {//        self.name=dict[@ "name"];//        self.icon=dict[@ "icon"];//        self.intro=dict[@ "Intro"];                Use KVC        [self setvaluesforkeyswithdictionary:dict];    }    return self;} + (Instancetype) heroswithdict: (nsdictionary *) dict{    return [[Self alloc]initwithdict:dict];}

Five, the attention point

1. Two steps to refresh data:

1) Modify the model

2) Refresh the table data (you can refresh all or refresh the specified row)

2. In the master controller file, three protocols are adhered to

respectively:

Uitableviewdatasource,

Uialertviewdelegate,

Uitableviewdelegate

iOS Development UI chapter-Implementing UITableView Control Data Refresh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.