General cell information

Source: Internet
Author: User

Recently, company projects often have this interface: A tableview, and each cell has a data, just like the interface when many user information needs to be filled in for website registration users. However, these cells are not the same. They can be set labels and cannot be edited. They can be the textfield to be input, the button pop-up window, or uiswitch. The reason why I want to write an article is that I think the appearance of this interface is regular, and I have found a good way to write this interface.

When will this interface be required? When the data you want to submit has multiple fields and each field requires user input, the key lies in user input. Similar to the improvement of user information, such as adding a resume on 51job, this interface is used when the submitted items have multiple attributes and each attribute needs to be input by the user. Introduction to csdn editing



It seems very simple, but it seems not that simple. The first problem was that the cell disappeared after the screen was drawn and then returned. The problem was summarized in another article.

Think about it. The biggest problem is whether to write each cell individually or use a common cell to implement different interfaces through different parameters. At first, I wrote each cell separately. It was really exhausting (I wrote a pure code interface ). There is a static tableview in the story board, but you cannot use the story board for a separate interface. Can you use XIB to write the tableview interface? Drag a tableview, set a delegate, and then run the code to write the code for every cell. Finally, the type of cell filled in information is limited, and the most important thing is that a cell is responsible for the content, as shown in the figure above, the first line is the "nickname" and "Real Name" content. It is estimated that there will be two lines on the mobile phone, that is, two cells. What are the benefits of this nature? That is, a cell has only one content. In this way, you can write a method to the cell to obtain the content. The cell will obtain the corresponding text based on its own type, for example:

-(Nsstring *) text {If (_ type = textfield) {return self. textfield. text;} else if (_ type = button) {return self. button. titlelabel. text;} else if (_ type = textview) {return self. textview. text;} else if (_ type = switchbtn) {return self. switchbtn. on? @ "Yes": @ "no";} else if (_ type = label) {return self. resultlabel. Text;} return nil ;}

_ Type is an enumeration type:

typedef enum{    textField = 0,    button,    label,    textView,    switchBtn,    inputNone}contentType;
You can also set text, that is, to use cell directly. TEXT = @ "China", the Cell will handle it on its own, saving the need to make repeated judgments. It's so nice to simplify it!

Each of the above enumeration types represents a method for carrying the cell content. After such processing, this cell is an abstract and more general cell, it is to sum up all the different cells into one. Many problems have become simple: 1. You can set the font in a unified manner. 2. You can build cells in batches. For example:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier contentType:(contentType)type title:(NSString*)title text:(NSString*)text placeHolder:(NSString*)placeHolder indexPath:(NSIndexPath*)indexPath;
This is the cell init method, and then prepare the type array, title array, text array, and so on. Of course, you can also encapsulate these in a data class or dictionary and then load them into an array, then, retrieve the parameters required for each cell based on indexpath and send them to the init method. You don't need to worry about other parameters.


Of course, I think the most convenient thing is that when submitting data: 1. Check whether the data is empty. Check every item of content and try again. Then I will pop up and remind you that I am tired of thinking about it, if a common cell is used, you can write a check method for the cell, such

// Check whether the content is input at the time of submission-(bool) checkcontent {// The form of inputnone is that there is no content input, so do not check whether the content is empty if (! _ Isneedcheck | _ type = inputnone) {return yes;} If (self. TEXT = nil | [self. text isequaltostring: @ ""]) {nsstring * alerttext = nil; If (self. type = button) {alerttext = [nsstring stringwithformat: @ "select % @", _ title];} else {alerttext = [nsstring stringwithformat: @ "Enter % @", _ title];} [svprogresshud showerrorwithstatus: alerttext]; return no;} return yes ;}
Commit only needs to save the cell array cyclically, and each cell can call the checkcontent method.

2. When submitting data, for example, constructing a JSON string, the above response must be {"nickname": @ "XXX", @ "Real Name": @ "yyy ", @ "position": @ "Zzz"} in this form, you need to get each key and the corresponding value. Is it exhausting, if these are not placed in a regular order (in fact, the rule is put into the array in order, or the key value is saved in the dictionary ). If a general cell is used and these cells are stored in the same array, you only need to loop through this array:

NSMutableDictionary* jsonDic = [NSMutableDictionary dictionary];    for (int i = 0; i<_cellArray.count; i++) {        TaskItemCustomCell* cell = [_cellArray objectAtIndex:i];        [jsonDic setValue:cell.text forKey:cell.key];    }
Easy to believe! Cell. text is the content above the cell. The cell gets the corresponding text based on its own type, and the key is put during cell construction, this is also because a cell is only responsible for one item, so that a cell corresponds to a key-value pair in the JSON Dictionary of the submitted data. In a loop, store all the cell content and corresponding keys in the dictionary. Everything is done!


The reason for doing so is that the nature of the interface itself is determined by the nature of tableview!

In addition, if there is a new content input form, you only need to add a new type to the cell, without affecting the original state.



General cell information

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.