日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

代码优化之减少重复代码-实践

發布時間:2025/3/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 代码优化之减少重复代码-实践 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景

在APP中列表是一種比較常見的數據展示方式,當有數據時,就顯示數據;如果沒有數據,一般不會顯示一個空白頁面,而是在空白頁面上加一些提示信息,比如像下面這樣:

不同的APP會有不同的設計,但不管是什么樣的設計,它在整個APP內部應該是一致的,要變也只是文字或圖片稍有不同。

現狀

因為我們目前的項目還算比較龐大,所以這種列表無數據的情況出現了20多次,所以類似下面的代碼出現了就有20多次。為什么說類似,因為是由不同的人寫的,邏輯也是差不多,但真的各不相同,有的封裝成一個方法,比如:setNoMessageView,有的直接寫在viewDidLoad里面......

- (void)setNoMessageView{self.noInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 45, SCREEN_WIDTH, SCREEN_HEIGHT)];self.noInfoView.backgroundColor = [UIColor clearColor];[self.view addSubview:self.noInfoView];UIImageView *carImageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-120)/2, 60, 120, 86)];[carImageView setImage:[UIImage imageNamed:@"no_message.png"]];[self.noInfoView addSubview:carImageView];UILabel *noInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, SCREEN_WIDTH, 20)];noInfoLabel.textAlignment = NSTextAlignmentCenter;noInfoLabel.textColor = LCRGBColor(211, 211, 211);noInfoLabel.text = NSLocalizedString(@"Dear, no information", nil);noInfoLabel.backgroundColor = [UIColor clearColor];noInfoLabel.font = [LCFont systemFontOfSize:20];[self.noInfoView addSubview:noInfoLabel];}

先不考慮重復的問題,只是孤立的看上述代碼,它也有一些問題:

  • self.noInfoView的frame應該視根據上下文獲得的,而不是和屏幕大小綁定,而且yOffset是45也是不對的。
  • carImageView的frame是固定大小的,而圖片有可能變。

第一個解決辦法

因為創建noInfoView的代碼基本差不多,我們可以封裝出一個Util方法。

+ (UIView*)createNoMessageViewWithFrame:(CGRect)frame image:(UIImage*)image text:(NSString*)text {UIView* noMessageView = [[UIView alloc] initWithFrame:frame];noMessageView.backgroundColor = [UIColor clearColor];UIImageView *carImageView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-image.size.width)/2, 60, image.size.width, image.size.height)];[carImageView setImage:image];[noMessageView addSubview:carImageView];UILabel *noInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, frame.size.width, 20)];noInfoLabel.textAlignment = NSTextAlignmentCenter;noInfoLabel.textColor = LCRGBColor(211, 211, 211);noInfoLabel.text = text;noInfoLabel.backgroundColor = [UIColor clearColor];noInfoLabel.font = [LCFont systemFontOfSize:20];[noMessageView addSubview:noInfoLabel];return noMessageView; }

調用:

- (void)setNoMessageView {CGRect rect = self.shopListView.frame;UIImage* image = [UIImage imageNamed:@"no_message.png"];NSString* text = NSLocalizedString(@"Dear, no information", nil);self.noInfoView = [HJUIUtil createNoMessageViewWithFrame:rect image:image text:text];[self.view addSubview:self.noInfoView]; }

這樣改看起來好多了,把共性封裝,把差異作為接口留出。然后其他地方原本要寫20行代碼,現在只要寫5行,而且多個地方調用的代碼不會有太大的出入,便于閱讀和理解。

第二個解決辦法

上面的辦法已經不錯了,不過除了寫5行代碼之外,還給ViewController增加了一個屬性:noInfoView?,F在仔細想一下noInfoView出現的原因,是因為TableView沒有內容顯示的時候,noInfoView才顯示出來,否則就隱藏??梢?#xff0c;這個noInfoView和TableView是緊密聯系的,我們可以從UITableView的狀態得出noInfoView的狀態。那為什么不把它綁定到UITableView上呢?好,那就給UITableView增加一個屬性,叫做emptyView。

給一個系統的類增加屬性不是那么的簡單,但是只要看過SVPullToRefresh的代碼,就知道怎么做了。
首先,給UITableView增加一個Category。

@interface UITableView(EmptyView)@property (nonatomic, strong, readonly) UIView *emptyView;-(void)addEmptyViewWithImageName:(NSString*)imageName title:(NSString*)title;@end static char UITableViewEmptyView;@implementation UITableView(EmptyView)@dynamic emptyView;- (UIView *)emptyView {return objc_getAssociatedObject(self, &UITableViewEmptyView); }- (void)setEmptyView:(UIView *)emptyView {[self willChangeValueForKey:@"HJEmptyView"];objc_setAssociatedObject(self, &UITableViewEmptyView,emptyView,OBJC_ASSOCIATION_ASSIGN);[self didChangeValueForKey:@"HJEmptyView"]; }-(void)addEmptyViewWithImageName:(NSString*)imageName title:(NSString*)title {if (!self.emptyView){CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);UIImage* image = [UIImage imageNamed:imageName];NSString* text = title;UIView* noMessageView = [[UIView alloc] initWithFrame:frame];noMessageView.backgroundColor = [UIColor clearColor];UIImageView *carImageView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-image.size.width)/2, 60, image.size.width, image.size.height)];[carImageView setImage:image];[noMessageView addSubview:carImageView];UILabel *noInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, frame.size.width, 20)];noInfoLabel.textAlignment = NSTextAlignmentCenter;noInfoLabel.textColor = LCRGBColor(211, 211, 211);noInfoLabel.text = text;noInfoLabel.backgroundColor = [UIColor clearColor];noInfoLabel.font = [LCFont systemFontOfSize:20];[noMessageView addSubview:noInfoLabel];[self addSubview:noMessageView];self.emptyView = noMessageView;}}@end

然后外部調用就很簡單了,沒有額外的屬性,而且一句話就搞定。

[self.shopListView.shopListTableView addEmptyViewWithImageName:@"no_message.png" title:@"Dear, no information"];

然后在加載數據前進行判定是否顯示emptyView

self.shopListView.shopListTableView.emptyView.hidden = ([self.dataArray count] != 0);

?

轉載于:https://www.cnblogs.com/yulang314/p/5084372.html

總結

以上是生活随笔為你收集整理的代码优化之减少重复代码-实践的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。