iOS開發之新浪微博山寨版代碼最佳化,ios開發新浪博山

來源:互聯網
上載者:User

iOS開發之新浪微博山寨版代碼最佳化,ios開發新浪博山

  之前發表過一篇部落格“IOS開發之新浪圍脖”,在編寫代碼的時候太偏重功能的實現了,寫完準系統後看著代碼有些彆扭,特別是用到的四種cell的類,重複代碼有點多,所以今天花點時間把代碼重構一下。為了減少代碼的重複編寫把cell中相同的部分抽象成父類,然後繼承。不過也是結合著storyboard做的。在最佳化時轉寄的View和評論的View相似,於是就做了個重用。在原來的代碼上就把cell的代碼進行了重寫,所以本篇作為補充,關鍵代碼還得看之前的部落格。

  1.第一種cell,只有微博內容,沒有圖片,效果如下:

1 #import <UIKit/UIKit.h> 2 3 //TableView要回調的block,用於把cell中的按鈕的tag傳給TableView 4 typedef void (^MyCellBlock) (UITableViewCell * cell, int tag); 5 6 @interface TextTableViewCell : UITableViewCell 7 //接收block塊 8 -(void)setMyCellBlock:(MyCellBlock) block; 9 10 //接收字典11 -(void) setDic:(NSDictionary *)dic;12 13 @end

  TextTableViewCell.m(帶圖片的cell繼承於這個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 block;12 13 @end14 15 @implementation TextTableViewCell16 17 //擷取傳入的block塊18 -(void)setMyCellBlock:(MyCellBlock)block19 {20     self.block = block;21 }22 23 //擷取傳入的參數,用於給我們的cell中的標籤賦值24 -(void) setDic:(NSDictionary *)dic25 {26     27     //設定頭像28    [self.headImage setImageWithURL:[NSURL URLWithString:dic[@"user"][@"profile_image_url"]]];29     30     //設定暱稱31     self.nameLabel.text = dic[@"user"][@"name"];32     33     //設定時間34     NSDateFormatter *iosDateFormater=[[NSDateFormatter alloc]init];35     iosDateFormater.dateFormat=@"EEE MMM d HH:mm:ss Z yyyy";36 37     //必須設定,否則無法解析38     iosDateFormater.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];39    NSDate *date=[iosDateFormater dateFromString:dic[@"created_at"]];40   41      //目的格式42      NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];43      [resultFormatter setDateFormat:@"MM月dd日 HH:mm"];44     self.dateLabel.text = [resultFormatter stringFromDate:date];45     46     //設定微博博文47     self.weiboTextLabel.text = dic[@"text"];48     49 }50 51 52 //通過block回調來返回按鈕的tag53 - (IBAction)tapCellButton:(id)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、上面的代碼有點多,如果我們再加第二個cell(原微博帶圖片的)就簡單多了,可以繼承與上面的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.第三種cell,是轉寄微博不帶圖片的,如下:

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 11 -(void)setDic:(NSDictionary *)dic12 {13 [super setDic:dic];14 //移除約束15 [self removeConstraint:self.textHeightConstraint];16 17 //給據text的值求出textLabel的高度18 NSString *text = dic[@"text"];19 NSDictionary * dic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};20 21 CGRect frame = [text boundingRectWithSize:CGSizeMake(260, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];22 23 //建立新的約束24 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 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.第四種cell就是轉寄帶圖片的啦,效果如下:

#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

 

  來看一下最終的運行效果:

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 }

 

  在轉寄頁面中用到啦一個TextView, 我們給鍵盤上添加了一個Toolbar來進行鍵盤的回收,代碼如下:

1     //TextView的鍵盤定製回收按鈕2     UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];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;

  在要回調的方法中回收鍵盤:

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

 


在IOS開發做項目中怎最佳化APP項目?使得APP運行速度更快?(至少三點 )

1.最佳化記憶體管理
2.對大資料量使用消極式載入
3.需要多次請求的的資料想辦法緩衝
 
開發之初是否存在過類似於A,B兩個團隊同台競爭?

您好,確實存在這種情況。你可以看一下這篇報道。

renwu.51zjxm.com/...2.html

希望對你有協助,求好評!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.