Unequal Height cell construction (ii)

Source: Internet
Author: User
Tags set background

I. Commentview module constructionCommentview style is divided into two kinds of 1.xib building interface 1.1 because the comment style is roughly the same, we can use the same xib to handle the 1.2 hottest reviews with a label to build 1.3 below the content 1.3.1 Text style comments, below also          With a label, you can set the label constraint to both left and right, so that the height will scale with the content, as long as the label is set to 0 lines 1.3.2 Sound style comment sound style comments. The height is fixed. Add a view in the text comment position (according to the data and then decide which style of comments to show, the above section is the same) and then add a label and button to the view. Note: The button's picture and text are spaced only Set the inner margin of the inner child controls if the label is constrained under View, the button does not constrain the view setting, in short, set one, and the other does not set the 2. Request data 2.1 When setting model properties , we're going to process the data

2.2 It can be seen that we need to get username   first to get TOP_CMT this array, and then get the user dictionary  2.3 we are accustomed to model-oriented development, so we get the first element of the TOP_CMT Array (dictionary)   Go to model  2.4 and then take the user dictionary from the dictionary and switch to another model  2.5 then we need to customize two models  2.6 we'd better define a model attribute in the outermost model to hold a model of the inner layer ( ITEM0   is also the comment model)   Why?      when taking the inner layer model properties, because the hierarchy is too deep, need to write a long code       to make such an optimization, When you take the properties of the inner layer model, it is convenient for many  2.7 how to convert the dictionary inside the array attribute in the model to a property in the   model       Rewrite the set method of the array element   just get the dictionary element in the array, Assign values to this property in the model       for rigor, determine if the array is empty
1 // Assigning a value to the TOP_CMT property of a model 2 -(void) SETTOP_CMT: (Nsarray *) TOP_CMT3{4     _TOP_CMT = TOP_CMT; 5     if (top_cmt.count) {6         _commentitem = top_cmt.firstobject; 7     }8}9  

3. How to change the dictionary (or dictionary attribute) of the array attribute in the model into the model?  there are two ways to  3.1 the first method, manually use the MJ framework to  3.2 the second method, let the MJ framework automatically help us to turn, ( The premise is that the MJ framework knows what type of model the attributes are going to be transferred to in the model. How does the  3.3 MJ framework automatically help us to transfer the inner layer model? Mjextension: If there is a model in the model, Mjextension will automatically help you convert the good @property (nonatomic, strong) Xtuseritem *user; Because when we define this dictionary, we tell MJ the framework, What type of model to convert the dictionary to      mjextension: if there is an array in the model, and the array is a dictionary, Mjextension won't automatically turn you around. Mjextension you can turn a dictionary into a model, but you need to tell him what type of model to convert the dictionary in the array to,  3.4 how to tell MJ what kind of model to convert the dictionary in the array to
1 // key: Which array needs to be converted 2 + (Nsdictionary *) Mj_objectclassinarray3{4     return @{ @" TOP_CMT ":@"xtcommentitem"}; 5 }6  

3.5 Mjextension Low Level implementation      3.5.1 MJ Bottom is the use of KVC to assign a value to the model           to traverse the key in the model, Then go to the dictionary to find the corresponding value to the key assignment           3.5.2 using KVC assignment, the model can have no key value in the dictionary, but the key in the model Must have the corresponding value in the dictionary, otherwise it will be error                MJ Framework does not have this problem,       & nbsp        KVC bottom-up implementation:       http://www.jianshu.com/p/45cbd324ea65               KVC is using the [item Setvaluesforkeyswithdictionary:dict] method to convert a dictionary to a model        setvaluesforkeyswithdictionary: Underlying implementation                 Facilitates all key value values in the dictionary. Assign      [dict enumeratekeysandobjectsusingblock:^ to the corresponding Key,value (ID   _nonnull key, id  _nonnull obj, BOOL * _nonnull stop) {             &N bsp;   assigning values to corresponding Key,value           SETVALUESFORkeyswithdictionary: Will call setvalue:forkeypath:          [item setValue:obj Forkeypath:key];     }];     setvalue:forkeypath: the underlying implementation:                setValue:forKeyPath:              1. Based on the key value compared to the current attribute, first see if there is a set method with the same key value. Call the Set method .               2. If not, then compare, there is no member property with the same key value, if any, it is directly assigned .                3. If not, then to compare, there is a key value with the same and underlined member properties, if any, to directly assign the value .                4. If not, call SetValue: (ID) value forundefinedkey:               5. If not implemented, direct error .       3.5.3 KVC requirements, the key defined in the model is worth the data type   Be sure to have the same data type as the value of key in the dictionary, or you will get an error           3.5.4 MJ Framework to automatically convert some key value data types to and from the dictionaryKey corresponds to the data type of value (provided that the two data types can be converted to each other)   4. Calculate the frame and height of the cell child control in the view model  
1   if(Item.commentitem) {//First judge whether there is the hottest comments, there are comments to calculate2CGFloat Commenth = the; Sound commentary, height is ok3 Note: Later as long as the judge string has no content, with length4         if(item.commentItem.content.length) {//There is the content, is the text comment5 calculates the height of a comment based on the height of the text6NSString *totalstr = [NSString stringWithFormat:@"%@:%@", item.commentitem.user.username,item.commentitem.content];7Texth = [Totalstr sizewithfont:[uifont systemfontofsize: -] Constrainedtosize:cgsizemake (TEXTW, maxfloat)].height;8Commenth = ++Texth;9         }TenCGFloat COMMENTW =TEXTW; OneCGFloat Commentx =margin; ACGFloat Commenty =_cellh; -_commentviewframe =CGRectMake (Commentx, Commenty, COMMENTW, commenth); -_CELLH = Cgrectgetmaxy (_commentviewframe) +margin; the}
     5. The display data can be judged based on the number of elements in the TOP_CMT array, whether the Commentview is displayed or whether the model is converted based on the elements in the array.two. Bottomview Module Construction  This interface is relatively simple to build, do not specifically introduce the  1.xib build interface  2. Request Data      2.1 View interface documentation, use AFN to send network requests       2.2 when a network request succeeds, convert the data into models       2.3 define attributes in the model (Customize the model first)            See what data the interface needs to use, find the definition of the data that defines the       2.3 custom view model in the model, the view model contains the model ( is to define the properties of a model in a view model)       2.4 Traverse model, convert all to view model           CREATE VIEW model object           Use the model properties of the view model to receive the model           to save the view model to an array of  3. Calculate the frame and height of the cell child control in the view model       height give a fixed value on the line, the cell's height is the y-axis direction the last control's maximum y-value  4. Presentation Data            4.1 is the same way of showing data, it's mostly about data processing            4.2 When the number of comments or likes more than 10,000 to deal with           4.2.1 More than 10,000, let the total divided by 10,000, the resulting number is retained a decimal place, display xx.x       nbsp   4.2.2 If the total is 0, display on the interface, the effect is poor, we have to do some processing                 If the data is 0, we display the placeholder in the interfaceText  
1 processing data source code2- (void) SetItem: (Xtthemeitem *) Item3 {4 [Super Setitem:item];5   6[Self Setbutton:_dingview count:item.ding title:@"likes"];7[Self Setbutton:_caiview count:item.cai title:@"Tread"];8[Self Setbutton:_shareview count:item.repost title:@"forwarding"];9[Self Setbutton:_commentview count:item.comment title:@"Reviews"];Ten } One  A- (void) Setbutton: (UIButton *) button count: (Nsinteger) Count title: (NSString *) Title - { - How to extract a method: First to extract the source code of the method to copy over, what is missing what to do, need outside the decision of things, written parameters, let the outside pass in theCGFloat Valuef =0; -NSString *str =title; -     if(Count >10000.0) { -Valuef = Count/10000.0; +str = [NSString stringWithFormat:@"%.1f million", Valuef]; -str = [STR stringbyreplacingoccurrencesofstring:@". 0"Withstring:@""]; +}Else if(Count >0) { Astr = [NSString stringWithFormat:@"%ld", Count]; at     } - [button Settitle:str forstate:uicontrolstatenormal]; - } -  

Three. Handling the cell's appearance details the selected style of 1.cell is ugly, how do we cancel? 1.1 Set a property on the cell to do it.
     Self.selectionstyle = Uitableviewcellselectionstylenone;
1.2 Where to write code? In the Initialize cell method, write
-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier

2. We want to set the width of the split line, how to set? First, we need to remove the split line from the system first.
Self.tableView.separatorStyle = Uitableviewcellseparatorstylenone;
      2.1 Custom Split line           Add a uiview to the bottom of the cell and set the height and color to achieve the split line effect    & nbsp  2.2 set the cell's frame, let the cell's height reduce a value, the reduced part can see the background color, only need to set the background color to achieve the purpose of setting the split line           NOTE: Here the reduced value , be sure to add the cell's height when you set it, or the cell may be compressed  
 1   set frame source code  2 -(void  ) Setframe: (cgrect) frame  3  { 4  frame.origin.y + = 10  ; 5  frame.size.height-= ; 6   Note: Be sure to call the Super method  7   [Super Setframe:frame];  8 } 
      2.3 Set background image for cell            Set a background image to the cell via the Backgroundview property. You only need to set Uiimageview to the cell's Backgroundview.            Set up the discovery, the edge of the background image is a bit ugly, it is possible that the picture has been stretched, We're going to process the picture            Set the stretch area of the picture, and then set the cell's background image  
1 Set cell background image source code 2  UIImage *image = [UIImage imagenamed:@ "maincellbackground"]; 3 working with pictures: setting an extruded area 4 0.5 0.5 ]; 5  6 Self.backgroundview = [[Uiimageview alloc] initwithimage:image];


Unequal Height cell construction (ii)

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.