What is UITableView
? In many mobile applications, you can see a wide variety of tabular data
in the IOS , the most common practice for tabular data presentation is to use UITableView
UITableView inherit from Uiscrollview , so vertical scrolling is supported and performance is excellent
UITableView the two styles
How to display data
? UITableView requires a data source (DataSource) to display the data?? UITableView queries the data source for the total number of rows of data and what data is displayed for each row.?? UITableView with no data source set is just an empty shell .?? Any OC object that complies with the Uitableviewdatasource protocol can be a UITableView data source
TableView and data Sources
TableView the process of presenting the data
1. Call the data source the following method to know how many sets of data
-(Nsinteger) Numberofsectionsintableview: (uitableview*) TableView;
2. Call the following method of the data source to know how many rows of data each group has
-(Nsinteger) TableView: (uitableview*) TableView numberofrowsinsection: (nsinteger) Section
3. Call the following method of the data source to know what each row shows
-(uitableviewcell*) TableView: (uitableview*) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath;
Dictionary Turn model
Initial MVC
? MVC is a design idea that runs through the entire iOS development and requires a certain amount of project experience in order to understand the implications and benefits.? Three characters in MVC? M:model, model data? V:view, view (interface)? C:control, control center.?? Some of the obvious features and embodiment of MVC:? What is shown above the view depends on the model .? As long as the model data is changed, the display state of the view will change? Control is responsible for initializing the model and passing the model to the view to parse the display
Cell Introduction
? Each line of UITableView is a UITableViewCell, initialized by DataSource's Tableview:cellforrowatindexpath: method to initialize each row? There is a default child view inside the UITableViewCell: Contentview,contentview is the parent view of the content displayed by the UITableViewCell, which displays some auxiliary indication views?the function of the secondary indicator view is to display an icon that represents an action, which can be displayed by setting the Accessorytype of the UITableViewCell, which is uitableviewcellaccessorynone by default (no secondary indicator view is displayed). The other values are as follows:UuitableviewcellaccessorydisclosureindicatoruUuitableviewcellaccessorydetaildisclosurebuttonuUuitableviewcellaccessorycheckmarku? You can also customize the secondary indicator view via the cell's Accessoryview property (for example, put a switch to the right)
UITableViewCell of the Contentview
There are 3 sub-views by default under Contentview? 2 of these are Uilabel (accessed via UITableViewCell's Textlabel and Detailtextlabel properties)the 3rd one is Uiimageview (accessed via UITableViewCell's ImageView property)? UITableViewCell also has a Uitableviewcellstyle property that determines which child views of Contentview are used and where they are located in Contentview
Cell the principle of reuse
iOS devices have limited memory, and if you use UITableView to display thousands of data, you need thousands of UITableViewCell objects, which will deplete the memory of your iOS device. To work around this problem, you need to reuse the UITableViewCell object?? Reuse principle : When scrolling the list, some UITableViewCell will move out of the window, and UITableView will place the UITableViewCell outside the window into an object pool for reuse. When UITableView requires DataSource to return UITableViewCell, DataSource will look at the object pool first, if there are unused uitableviewcell in the pool, DataSource will configure the UITableViewCell with the new data and return it to the UITableView and back to the window to avoid creating new objects
? There is also a very important question: Sometimes you need to customize UITableViewCell (inherit UITableViewCell with a subclass), and each row is not necessarily the same uitableviewcell, So a uitableview may have different types of uitableviewcell, and there will be many different types of UITableViewCell in the object pool, Then UITableView may get the wrong type when reusing UITableViewCell UITableViewCell?? solution : UITableViewCell has a nsstring*reuseidentifier attribute, You can set Reuseidentifier (typically UITableViewCell class name) by passing in a specific string identifier when initializing UITableViewCell. When UITableView requires DataSource to return UITableViewCell, a string is first identified to the object pool to find the corresponding type of UITableViewCell object, if there is, reuse, if not, The string identifier is passed in to initialize a UITableViewCell object?
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ //1. Defines the identity of a cell static nsstring *id = @ "Mjcell"; 2. Remove the cell uitableviewcell *cell = [TableView Dequeuereusablecellwithidentifier:id] from the cache pool; 3. If the buffer pool does not have a cell if (cell = = nil) { cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id]; } 4. Set the properties of the cell ... return cell;}
to encapsulate a view using xib
1. Create a new Xib file describing the internal structure of a view (assumed to be called mjtgcell.xib)●2. Create a new custom class
(Custom classes need to inherit from the system's own view, which class to inherit from, depending on the Xib root object's Class)
●3. The class name of the new class is best aligned with the Xib file name (for example, the class name is called Mjtgcell)●4. Connect the control in Xib and the. m file of the custom class●5. Provide a class method to return a created custom view (masking the process of loading from xib)●6. Provide a model attribute to let the outside world Pass model data●7. Override the setter method of the Model property, where the model data is presented to the corresponding child controls
D Elegate Use of the occasion
? Object A something happened inside a , want to notify object B ?? Object B wants to listen to what's going on inside object A .?? Object a you want to invoke a method of object b inside of your own method, and object A cannot have a coupling dependency on object B?? Object A want to pass data to object B ??......?
The result is the same: object B is the proxy for object A (delegate)
Use Steps to Delegate
1. Find out who is Who's agent first (delegate)●2. Define proxy protocols, naming conventions for protocol names: control class name +Delegate ●3. Defining Proxy Methods? Proxy methods are generally defined as @optional the proxy method names start with the control name? The proxy method has at least 1 parameters, passing the control itself out?4. Set Proxy (delegate) object (e.g. myview.delegate = xxxx;)? Proxy Object Compliance Protocolthe method of implementing the Proxy object Implementation protocol?5. Invoke the Proxy method at the right time for the proxy object (delegate) to notify the agent what has happened
(Determines whether the agent implements the proxy method before calling)
Customizing by Code cell (cell height is inconsistent )
1. Create a new class that inherits from UITableViewCell
2. Overriding Initwithstyle:reuseidentifier: Methods
? Add all the child controls that need to be displayed (you don't need to set the child control's data and frame, and the child controls are added to the Contentview)? Make child controls one-time property settings (some properties only need to be set once, such as fonts \ fixed pictures)
3.2 Models available
? Data model: Storing text data \ picture Datathe frame model: the height of the Frame\cell that holds the data model \ All child controls
4.cell has a frame model (don't have a data model directly)
5. Override the setter method of the Frame model property: Set the child control's display data and frame in this method
The initialization of the 6.FRAME model data has been done in a lazy manner (each cell corresponds to the frame model data is loaded only once)
10.ios of UITableView