Sina Weibo code optimization for iOS development

Source: Internet
Author: User

Previously published a blog "The development of iOS Sina scarf", in writing code too much emphasis on the implementation of the function, after the basic function to look at the code is somewhat awkward, especially the use of the four kinds of cell class, repeated code a bit more, so today spend some time to refactor the code. To reduce the repetition of code, the same part of the cell is abstracted into a parent class and then inherited. But it's also a combination of storyboard. The view that was forwarded during optimization was similar to the view of the comment, so it was reused. The code in the original code to rewrite the cell, so this article as a supplement, the key code also depends on the previous blog.

1. The first cell, only micro-blog content, no pictures, the effect is as follows:

The code for the cell corresponds to the following:

TextTableViewCell.h

1 #import  2  3//tableview The block to be recalled to pass the tag of the button in the cell to TableView 4 typedef  VOID (^mycellblock) ( UITableViewCell * cell, int tag); 5  6 @interface Texttableviewcell:uitableviewcell 7//Receive block block 8-(void) Setmycellblock: (mycellblock) block; 9 10//Receive dictionary one-(void) Setdic: (nsdictionary *) dic;12 @end

TEXTTABLEVIEWCELL.M (the cell with the image inherits from this cell)

 1 #import "TextTableViewCell.h" 2 3 @interface Texttableviewcell () 4 5 @property (strong, nonatomic) Iboutlet Uiimagevi EW *headimage; 6 @property (Strong, nonatomic) Iboutlet UILabel *namelabel; 7 @property (Strong, nonatomic) Iboutlet UILabel *datelabel; 8 @property (Strong, nonatomic) Iboutlet UILabel *weibotextlabel; 9 @property (Strong, nonatomic) nsdictionary *dic;11 @property (Strong, nonatomic) Mycellblock block;12 @end14 @i Mplementation TextTableViewCell16 17//Get incoming block block (void) Setmycellblock: (Mycellblock) block19 {self.block = block ; 21}22 23//Gets the parameter passed in to assign a value to the tag in our cell (void) Setdic: (Nsdictionary *) dic25 {26 27//Set avatar [Self.headimage SE Timagewithurl:[nsurl urlwithstring:dic[@ "user"][@ "profile_image_url"]]];29 30//Set Nickname Self.nameLabel.text = d ic[@ "user"][@ "name"];32 33//Set time NSDateFormatter *iosdateformater=[[nsdateformatter alloc]init];35 [EMA Il protected] "EEE MMM D HH:mm:ss Z yyyy"; 36 37//must be set, otherwiseUnable to parse Iosdateformater.locale=[[nslocale alloc]initwithlocaleidentifier:@ "en_US"];39 nsdate *date=[iosDateFormater datefromstring:dic[@ "Created_at"]];40 41//Destination format NSDateFormatter *resultformatter=[[nsdateformatter alloc]in it];43 [Resultformatter setdateformat:@ "MM month DD day hh:mm"];44 self.dateLabel.text = [Resultformatter stringfromdate:d ATE];45 46//Set micro Bobovin Self.weiboTextLabel.text = dic[@ "text"];48 49}50 51 52//block callback to return the button tag53-(IB Action) Tapcellbutton: (ID) Sender {UIButton *button = sender;55 self.block (self, button.tag);}57-(void) AW     akeFromNib59 {//initialization code61}62-(void) setselected: (BOOL) selected animated: (bool) ANIMATED64 {65 [Super setselected:selected animated:animated];66//Configure The view for the selected state68}69 @end

2, the above code a bit more, if we add a second cell (the original micro-Bo picture) is much simpler, you can inherit the cell above

  

The code for IMAGETABLEVIEWCELL.M is as follows: (add only the things you want added, not the code is much less)

@interface Imagetableviewcell () @property (Strong, nonatomic) Iboutlet Uiimageview *contentimage; @end @implementation imagetableviewcell-(void) Setdic: (nsdictionary *) dic{    [Super Setdic:dic];    [Self.contentimage setimagewithurl:[nsurl urlwithstring:dic[@ "Thumbnail_pic"]];} @end

3. The third cell, which is forwarded Weibo without pictures, is as follows:

Retexttableviewcell also inherits from Texttableviewcell. The code for RETEXTTABLEVIEWCELL.M is as follows:

 1 @interface Retexttableviewcell () 2 @property (Strong, nonatomic) Iboutlet UILabel *weibotextlabel; 3 @property (Strong, nonatomic) Iboutlet nslayoutconstraint *textheightconstraint; 4 5 @property (strong, nonatomic) Iboutlet Uitextview *retextview;     6 7 @end 8 9 @implementation ReTextTableViewCell10-(void) Setdic: (Nsdictionary *) DIC12 {[Super setdic:dic];14 Remove constraint [self removeconstraint:self.textheightconstraint];16 17//To the value of text to find Textlabel height of NSString *  Text = dic[@ "text"];19 nsdictionary * dic1 = @{nsfontattributename: [Uifont systemfontofsize:14]};20] CGRect frame = [Text Boundingrectwithsize:cgsizemake (260, Options:nsstringdrawinguseslinefragmentorigin) attributes: Dic1 context:nil];22 23//Create new constraint NSString *heightvalue = [NSString stringwithformat:@ "V:[_weibotextlabel (%LF )] ", frame.size.height+10];25 nsarray *constraint = [Nslayoutconstraint constraintswithvisualformat:heightvalue options:0 metrics:nil Views:Nsdictionaryofvariablebindings (_weibotextlabel)];26 self.textheightconstraint = constraint[0];28 [self AddC onstraint:self.textheightconstraint];29 Self.weiboTextLabel.text = text;31 = Self.reTextView.text = di c[@ "Retweeted_status"][@ "text"];33}35 @end

4. The fourth type of cell is the forwarding band picture, the effect is as follows:

Since the fourth cell is more than a third cell, it is inherited from the third cell, the code is as follows:

#import "ReImageTableViewCell.h" @interface Reimagetableviewcell () @property (Strong, nonatomic) iboutlet Uiimageview * Contentimageview; @end @implementation reimagetableviewcell-(void) Setdic: (nsdictionary *) dic{    [Super Setdic:dic ];    [Self.contentimageview setimagewithurl:[nsurl urlwithstring:dic[@ "Retweeted_status"][@ "Thumbnail_pic"]];} @end

Take a look at the final running effect:

By the above interface can clearly see the forwarding and comment interface is basically consistent, so we can use a viewcontroller in the code to control the view, by clicking on different buttons to stitch different URLs. The business logic selected is as follows:

1     if ([Self.tag isequaltovalue:@2]) 2     {3         [self post:comments_create content:@ "comment"];4     }5     if ([Self.tag isequaltovalue:@1]) 6     {7         [self post:repost_test content:@ "status"];8     }

In the forwarding page used to a textview, we add a toolbar to the keyboard for the recovery of the keyboard, the code is as follows:

1     //textview Keyboard Custom Recycle button 2     uitoolbar * ToolBar = [[Uitoolbar alloc]initwithframe:cgrectmake (0, 0, +)];3     4     Uibarbuttonitem * item1 = [[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemdone Target : Self action: @selector (tapdone:)];5     uibarbuttonitem * item2 = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:nil action:nil];6     UIBarButtonItem * Item3 = [[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace Target:nil Action: Nil];7     toolbar.items = @[item2,item1,item3];8     9     self.commentsTextView.inputAccessoryView =toolbar;

Recycle the keyboard in the method you want to callback:

1-(Ibaction) Tapdone: (ID) Sender {2     [Self.commentstextview resignfirstresponder];3}

Sina Weibo code optimization for iOS development

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.