Implementing UITableView Control Data Refresh
I. Project document structure and plist documents
Second, realize the effect
1. Description: This is a hero display interface, click on the selected row, you can change the name of the row hero (complete data refresh operation).
Run the interface:
Click on the selected line:
Automatically refresh after modifying data:
Third, code example
Data Model section:
YYheros.h file
Copy Code code as follows:
//
YYheros.h
10-Hero Show (data refresh)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Global.h"
@interface Yyheros:nsobject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *intro;
-(Instancetype) Initwithdict: (Nsdictionary *) dict;
+ (Instancetype) heroswithdict: (Nsdictionary *) dict;
Yyinith (Hero)
@end
YYHEROS.M file
Copy Code code as follows:
//
Yyheros.m
10-Hero Show (data refresh)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYheros.h"
@implementation Yyheros
-(Instancetype) Initwithdict: (Nsdictionary *) dict
//{
if (Self=[super init]) {
self.name=dict[@ "Name"];
self.icon=dict[@ "icon"];
self.intro=dict[@ "Intro"];
//
Using KVC
[Self setvaluesforkeyswithdictionary:dict];
// }
return self;
//}
//
+ (Instancetype) heroswithdict: (Nsdictionary *) dict
//{
return [[Self alloc]initwithdict:dict];
//}
YYINITM (Hero)
@end
Master Controller yyviewcontroller.m File
Copy Code code as follows:
//
Yyviewcontroller.m
10-Hero Show (data refresh)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYViewController.h"
#import "YYheros.h"
@interface Yyviewcontroller () <UITableViewDataSource,UIAlertViewDelegate,UITableViewDelegate>
@property (Strong, nonatomic) Iboutlet UITableView *tableview;
@property (Nonatomic,strong) Nsarray *heros;
@end
Copy Code code as follows:
@implementation Yyviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
Setting up a 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 *fullpath=[[nsbundle mainbundle]pathforresource:@ "Heros.plist" oftype:nil];
Nsarray *temparray=[nsarray Arraywithcontentsoffile:fullpath];
Nsmutablearray *arraym=[nsmutablearray Array];
For (Nsdictionary *dict in Temparray) {
Yyheros *hero=[yyheros Heroswithdict:dict];
[Arraym Addobject:hero];
}
_heros=[arraym Mutablecopy];
}
return _heros;
}
Treatment of #pragma mark-tableview
How many groups
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
return 1;
}
How many lines
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return self.heros.count;
}
Each set of data for each row, set the cell
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
NSLog (@ "Cellforrowatindexpath modified%d", indexpath.row);
1. Go to the cache to fetch
Static NSString *identifier=@ "Hero";
UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:identifier];
2. If not, then create your own
if (Cell==nil) {
NSLog (@ "Chuangjiancell");
Cell=[[uitableviewcell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifier];
}
3. Set up data
3.1 The model to get the line
Yyheros *hero=self.heros[indexpath.row];
Cell.textlabel.text=hero.name;
Cell.imageview.image=[uiimage ImageNamed:hero.icon];
Cell.detailtextlabel.text=hero.intro;
4. Return cell
return cell;
}
#pragma mark-Data Refresh
1. pop-up window, get the data
Call this method when a row is selected
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
Get the data model for a row
Yyheros *hero=self.heros[indexpath.row];
Uialertview *alert=[[uialertview alloc]initwithtitle:@ "Modify data" Message:nil delegate:self "Cancel" otherbuttontitles:@ "OK", nil];
Password Box form of
Alert.alertviewstyle=uialertviewstylesecuretextinput;
Alert.alertviewstyle=uialertviewstyleplaintextinput;
Uitextfield *text=[alert textfieldatindex:0];
Display the hero data for the current line in a text box
Text.text=hero.name;
Alert.tag=indexpath.row;
[Alert show];
}
2. Modify the data to complete the refresh operation
-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex
{
1. Modify the Model
If the check is canceled, then return without any action
if (0==buttonindex) return;
Otherwise modify the model and refresh the data
Yyheros *hero=self.heros[alertview.tag];
Get the text data in the current window (the data that has been modified)
Uitextfield *text=[alertview textfieldatindex:0];
Modify the model with modified data
Hero.name=text.text;
2. Refresh Data
All methods of the data source are automatically invoked whenever the TableView method is invoked
will automatically invoke Numberofsectionsintableview
will automatically invoke Numberofrowsinsection
will automatically invoke Cellforrowatindexpath
[Self.tableview Reloaddata];
Refreshes the specified line
Nsindexpath *path = [Nsindexpath indexPathForRow:alertView.tag insection:0];
[Self.tableview Reloadrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationright];
What happens if you don't refresh? (not refreshed immediately after the modification, until the cell is populated again)
}
Hide Status bar
-(BOOL) Prefersstatusbarhidden
{
return YES;
}
@end
Encapsulate the commonly used code into a macro with parameters
Encapsulating methods and code:
Copy Code code as follows:
//
Global.h
10-Hero Show (data refresh)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#ifndef _0____________global_h
#define _0____________global_h
/**
* Customize macros with parameters
*/
#define YYINITH (name)-(Instancetype) Initwithdict: (Nsdictionary *) dict;\
+ (Instancetype) heroswithdict: (Nsdictionary *) dict;
#define YYINITM (name)-(Instancetype) Initwithdict: (Nsdictionary *) dict\
{\
if (Self=[super init]) {\
[Self setvaluesforkeyswithdictionary:dict];\
}\
Return self;\
}\
\
+ (Instancetype) heroswithdict: (Nsdictionary *) dict\
{\
return [[Self alloc]initwithdict:dict];\
}\
#endif
In the future when you need to use, you just need to use a macro.
If you use the YYINITM (Hero) code in the YYHEROS.M file, you can replace the following code snippet:
Copy Code code as follows:
-(Instancetype) Initwithdict: (Nsdictionary *) dict
{
if (Self=[super init]) {
self.name=dict[@ "Name"];
self.icon=dict[@ "icon"];
self.intro=dict[@ "Intro"];
Using KVC
[Self setvaluesforkeyswithdictionary:dict];
}
return self;
}
+ (Instancetype) heroswithdict: (Nsdictionary *) dict
{
return [[Self alloc]initwithdict:dict];
}
Five, Attention point
1. Two steps to refresh data:
1) Modify the model
2 Refresh the table data (you can refresh all, or you can refresh the specified row)
2. In the master controller file, three protocols are adhered to
respectively:
Uitableviewdatasource,
Uialertviewdelegate,
Uitableviewdelegate
UITableView Control Usage Summary
First, the use of UITableView steps
The use of UITableView has only a simple three steps:
1. Tell a total number of data sets
Method:
Copy Code code as follows:
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView;
2. Tell each group how many lines there are
Method:
Copy Code code as follows:
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section;
3. Set each row of each group (cell)
Method:
Copy Code code as follows:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath;
Second, the use of instructions
1. How many sets of data and how many rows are shown are usually closely related to the data, and in development the data is usually stored in the plist file and needs to be loaded into the project (model) in a certain way.
2. Set each row of each group, say simple point is to set tableview each cell.
There are three steps to setting up a cell:
(1) Create a cell (need to consider the performance of the cell for recycling, pay attention to the caching process)
(2) Setting up data for the cell
(3) Return a cell
There are three different ways to set up a cell:
(1) using the Tableviewcell provided by the system to set up
(2) through Xib custom Tableviewcell, apply long consistent, such as the group purchase display interface
(3) through the Pure Code custom Tableviewcell, applies to has the disparity, if behaves is highly different, some cell has certain attribute, but some cell does not have, like the Micro-blog display interface
Three, custom Tableviewcell
1. To customize a view through the Xib file
(1) Create a new Xib file that describes the interior of a view
(2) Create a new custom class, the custom class needs to inherit from the system's own view, which class to inherit from, depending on xib and object class
(3) The type of the new class is best kept consistent with the Xib file name
(4) Connect the Xib control with the. m file of the custom class
(5) A method of providing a class returns a created custom iview (masking the process of loading from xib)
(6) Provide a model attribute to allow the outside world to pass model data
(7) Rewrite the setter method of the Model property, where the model data is presented to the corresponding child control
2. Customize the cell by code
(1) New ⼀ A class that inherits from UITableViewCell
(2) Rewrite Initwithstyle:reuseidentifier: Method
Add all child controls that need to be displayed (you do not need to set the data and frame of the child controls, and the child controls are added to the Contentview)
A one-time property setting for the child control (some properties only need to be set once, such as fonts \ fixed pictures)
(3) Provide 2 models
Data model: Storing text data \ image data
Frame model: The height of the Frame\cell that holds the data model \ All child controls
(4) The cell has a frame model (do not have the data model directly)
(5) Setter method to override the property of the frame model: Set the display data and frame of the child control in this method
(6) The initialization of the frame model data has been done in a lazy loading manner (each cell corresponding to the frame model data is loaded only once)
Iv. Procedures for using agents
(1) First find out who is Who's agent (delegate)
(2) Define the proxy protocol, the naming specification of the Protocol name: control class name + Delegate
(3) Defining proxy methods
Proxy methods are generally defined as @optional
Proxy method names begin with the control name
The proxy method has at least 1 parameters, passing the control itself out
(4) Set agent (Delegate) object (⽐ such as Myview.delegate = xxxx;)
Agent Object Compliance Protocol
The method of implementing the implementation of the Protocol within the proxy object
(5) In the appropriate time ⽤ proxy object (delegate) Agent method, notify the agent what happened
(Determine whether the agent implements the proxy ⽅ method before tuning ⽤)