IOS development: code optimization of Sina Weibo version; ios development: Sina boshan

Source: Internet
Author: User

IOS development: code optimization of Sina Weibo version; ios development: Sina boshan

I have published a blog titled "Sina Weibo for IOS development", which focuses too much on function implementation during code writing. After writing the basic functions, the code looks awkward, in particular, the four cell classes are used, and there are a lot of repeated code. So today I will spend some time refactoring the code. Abstract The same part of the cell into a parent class to reduce code duplication and then inherit from it. But it also works with storyboard. The View forwarded during optimization is similar to the View of the comment, so it is reused. The cell code is rewritten in the original code, so this article serves as a supplement, and the key code has to be viewed in the previous blog.

1. The first cell contains only Weibo content and no images. The effect is as follows:

1 # import <UIKit/UIKit. h> 2 3 // the block to be called back by TableView. It is used 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 // receiving block 8-(void) setMyCellBlock :( MyCellBlock) block; 9 10 // receiving dictionary 11-(void) setDic :( NSDictionary *) dic; 12 13 @ end

TextTableViewCell. m (cell with image inherits from this cell)

1 # import "TextTableViewCell. h "2 3 @ interface TextTableViewCell () 4 5 @ property (strong, nonatomic) IBOutlet UIImageView * headImage; 6 @ property (strong, nonatomic) IBOutlet UILabel * nameLabel; 7 @ property (strong, nonatomic) IBOutlet UILabel * dateLabel; 8 @ property (strong, nonatomic) IBOutlet UILabel * weiboTextLabel; 9 10 @ property (strong, nonatomic) NSDictionary * dic; 11 @ property (strong, nonatomic) MyCellBlock; 12 13 @ end14 15 @ implementation TextTableViewCell16 17 // obtain the incoming block 18-(void) setMyCellBlock :( MyCellBlock) block19 {20 self. block = block; 21} 22 23 // obtain the passed parameter, which is used to assign a 24-(void) setDic :( NSDictionary *) value to the tag in our cell *) dic25 {26 27 // set Avatar 28 [self. headImage setImageWithURL: [NSURL URLWithString: dic [@ "user"] [@ "profile_image_url"]; 29 30 // sets the nickname 31 self. nameLabel. text = dic [@ "user"] [@ "name"]; 32 33 // set time 34 NSDateFormatter * iosDateFormater = [[NSDateFormatter alloc] init]; 35 iosDateFormater. dateFormat = @ "eee mmm d HH: mm: ss Z yyyy"; 36 37 // required; otherwise, 38 iosDateFormater cannot be parsed. locale = [[NSLocale alloc] initWithLocaleIdentifier: @ "en_US"]; 39 NSDate * date = [iosDateFormater dateFromString: dic [@ "created_at"]; 40 41 // target format 42 NSDateFormatter * resultFormatter = [[NSDateFormatter alloc] init]; 43 [resultFormatter setDateFormat: @ "MM dd HH: mm"]; 44 self. dateLabel. text = [resultFormatter stringFromDate: date]; 45 46 // set Weibo blog 47 self. weiboTextLabel. text = dic [@ "text"]; 48 49} 50 51 52 // use block callback to return the tag53-(IBAction) tapCellButton (id) of the button) sender {54 UIButton * button = sender; 55 self. block (self, button. tag); 56} 57 58-(void) awakeFromNib59 {60 // Initialization code61} 62 63-(void) setSelected :( BOOL) selected animated :( BOOL) animated64 {65 [super setSelected: selected animated: animated]; 66 67 // Configure the view for the selected state68} 69 70 @ end

 

2. The above code is a bit too much. If we add a second cell (the original Weibo image), it will be much simpler. We can inherit from the above cell.

@ 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 type of cell is to forward Weibo without pictures, as shown below:

1 @ interface ReTextTableViewCell () 2 @ property (strong, nonatomic) IBOutlet UILabel * weiboTextLabel; 3 @ property (strong, nonatomic) IBOutlet NSLayoutConstraint * handle; 4 5 @ property (strong, nonatomic) IBOutlet UITextView * reTextView; 6 7 @ end 8 9 @ implementation ReTextTableViewCell10 11-(void) setDic :( NSDictionary *) dic12 {13 [super setDic: dic]; 14 // remove constraint 15 [self removeConstraint: self. textHeightConstraint]; 16 17 // calculate the textLabel height for the text value 18 NSString * text = dic [@ "text"]; 19 NSDictionary * dic1 =@{ NSFontAttributeName: [UIFont systemFontOfSize: 14]}; 20 21 CGRect frame = [text boundingRectWithSize: CGSizeMake (260,100 0) options: NSStringDrawingUsesLineFragmentOrigin attributes: dic1 context: nil]; 22 23 // create a new constraint 24 NSString * heightValue = [NSString stringWithFormat: @ "V: [_ weiboTextLabel (% lf)]", frame. size. height + 10]; 25 NSArray * constraint = [NSLayoutConstraint constraintsWithVisualFormat: heightValue options: 0 metrics: nil views: sums (_ weiboTextLabel)]; 26 27 self. textHeightConstraint = constraint [0]; 28 [self addConstraint: self. textHeightConstraint]; 29 30 self. weiboTextLabel. text = text; 31 32 self. reTextView. text = dic [@ "retweeted_status"] [@ "text"]; 33 34} 35 @ end

 

4. The fourth type of cell is to forward images with the following effects:

# Import "ReImageTableViewCell. h "@ interface ReImageTableViewCell () @ property (strong, nonatomic) IBOutlet UIImageView * handle; @ end @ implementation ReImageTableViewCell-(void) setDic :( NSDictionary *) dic {[super setDic: dic]; [self. contentImageView setImageWithURL: [NSURL URLWithString: dic [@ "retweeted_status"] [@ "thumbnail_pic"];} @ end

 

Let's take a look at the final running effect:

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}

 

A TextView is used in the forwarding page. We add a Toolbar to the keyboard to recycle the keyboard. The Code is as follows:

1 // custom keyboard recycle button for TextView 2 UIToolbar * toolBar = [[UIToolbar alloc] initWithFrame: CGRectMake (0, 0,320, 30)]; 3 4 UIBarButtonItem * item1 = [[UIBarButtonItem alloc] lower: lower target: self action: @ selector (tapDone :)]; 5 UIBarButtonItem * item2 = [[UIBarButtonItem alloc] lower: required 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;

Reclaim the keyboard in the method to be called back:

1 - (IBAction)tapDone:(id)sender {2     [self.commentsTextView resignFirstResponder];3 }

 


How to optimize the APP project in IOS development projects? Is the APP running faster? (At least three points)

1. Optimized Memory Management
2. Use delayed loading for large data volumes
3. How to cache data that requires multiple requests

Have two teams, A and B, competed on the same stage at the beginning of development?

Hello, this is indeed the case. You can take a look at this report.

Renwu.51zjxm.com/..2.html

I hope it will help you!

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.