Data Binding Principles for iOS tableview

Source: Internet
Author: User

Data Binding Principles
  

If you are writing subclass code of the table View Controller, most of the code is in the uitableviewdelegte and uitableview-datasource methods. We focus on how to write concise method code to solve the problem. The cellforrowatindexpath method usually sets the value code for a specific UI element of each form element. Set the UI element in the form Element
The best way to do this is to move the code elsewhere. Where should I move it now? It depends on which type of custom form you are using.
RMB. Based on the functions that your application needs to achieve, the table view needs to be connected with the relevant data.

This technology is usually called data binding. It is not very important in iOS, at least compared with Mac.
In this way. The best way to bind data is to pass your data model object to a custom table view unit and bind it with data.

Table view units are divided into three types based on the associated data method. The first class is a subclass of uitableviewcell. it
Is a custom form element used to display specific types of data. Generally, it is bound to the specific information of the application. Display in RSS reader
The feedcell of push information is an example.

The role of the second type is similar to Apple's uitableviewcell implementation. You create the form elements of the specified style.
Meta can be used to display multiple types of data models in other classes or projects. For example, you can create an image
For example, mytableviewswitchcell can display the title text, uiswitch switch, or mytableviewinputcell
This can display the title text and the general class that displays the data item uitextfield text box.

The third form element is the native uitableviewcell provided by the uikit framework. Regardless of the type, try
The data binding code is moved to the form element itself.

The following is the first case, which is simple and intuitive. Compile the method for receiving model objects (as parameters) in feedcell
Set the value of the feed model object to a specific UI element. That is, move your data binding code
In feedcell. For example, in an RSS reader application, the feedcell public method looks like the following
In this way:

Feedcell binding method

-(Void) bind :( feed *) feedtobedisplayed {
Self. titlelabel. Text = feedtobedisplayed. text;
Self. timestamplabel. Text = feedtobedisplayed. modifieddatestring;

...
}

Instead of writing code in the data source method cellforrowatindexpath of the View Controller, we moved it
In the subclass of uitableviewcell. This means that if you need to modify the format of the form element (for example, you want to give the feed model object and
When feedcell adds the author name information, you can perform operations there (in a place.

If you use the default uitableviewcell to display data, we recommend that you add this binding method to uitable-
Viewcell classification class.

In the second case, you have multiple models that use the same uitableviewcell form element. We recommend that you create multiple category classes,
One model. For example, create a uitableviewcell + feed. h/m file to display the push information, or create a uitableview-
The cell + subscribe. h/m file displays subscription information on the same form element. Exercise caution when naming the binding method. For example
If the category contains a duplicate method name, it will overwrite the previously defined method, and the occurrence of this rewrite does not define the sequence.
We recommend that you use bind <modelclassname> to name them, which is easier to read and understand. For example
Bindfeed (feed *) and bindsubscription :( subscribe *) naming conventions.

In the third case, you have a general custom form element like mytableviewswitchcell. In this case
You can also use the technology mentioned earlier. Adds a classification method for a common custom table view unit.

In general, you may reuse the same feedcell cells in multiple tables and multiple view controllers. Bind to data
Removing the relevant code from the table View Controller (or any other General View Controller) can reduce code confusion in the Controller and
Simplify code maintenance.

Related Article

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.