iOS開發 自定義併發NSOperation實戰

2021-07-22 17:42:36 字數 4128 閱讀 7358

前一章節已經介紹了如何自定義併發nsoperation,本節將其應用到具體例項,如果自定義併發nsoperation不會,請移步:

在zccurrentoperation.h檔案中**如下:

//

// zccurrentoperation.h

// 自定義非併發nsoperation

//// created by mrzhao on 16/9/13.

///*

*自定義併發的nsoperation需要以下步驟:

1.start方法:該方法必須實現,

2.main:該方法可選,如果你在start方法中定義了你的任務,則這個方法就可以不實現,但通常為了**邏輯清晰,通常會在該方法中定義自己的任務

4.isconcurrent :必須覆蓋並返回yes;

*/#import #import @class zccurrentoperation;

@protocol currentoperationdelegate -(void)downloadoperation:(zccurrentoperation*)operation didfisheddownload:(uiimage *)image;

@end

@inte***ce zccurrentoperation : nsoperation

@property (nonatomic, copy)nsstring *urlstr;

@property (nonatomic, strong)nsindexpath *indexpath;

@property (nonatomic, weak)iddelegate;

@end

在zccurrentoperation.m檔案中**如下:

//

// zccurrentoperation.m

// 自定義非併發nsoperation

//

// created by mrzhao on 16/9/13.

//#import "zccurrentoperation.h"

@implementation zccurrentoperation

- (id)init

return self;

}- (bool)isconcurrent

- (bool)i***ecuting

- (bool)isfinished

- (void)start

//如果沒被取消,開始執行任務

[self willchangevalueforkey:@"i***ecuting"];

[nsthread detachnewthreadselector:@selector(main) totarget:self withobject:nil];

executing = yes;

[self didchangevalueforkey:@"i***ecuting"];

}- (void)main );

}nsthread *thread = [nsthread currentthread];

nslog(@"%@",thread);

//任務執行完成後要實現相應的kvo

[self willchangevalueforkey:@"isfinished"];

[self willchangevalueforkey:@"i***ecuting"];

executing = no;

finished = yes;

[self didchangevalueforkey:@"i***ecuting"];

[self didchangevalueforkey:@"isfinished"];}}

@catch (n***ception *exception)

}@end

控制器的**如下:

//

// viewcontroller.m

// 自定義併發nsoperation

//// created by mrzhao on 16/9/13.

//#import "viewcontroller.h"

#import "zccurrentoperation.h"

#import "zcnetworkingtool.h"

#import "mjextension.h"

#import "zcliveuser.h"

@inte***ce viewcontroller () @property (nonatomic, strong)nsmutablearray *datasource;

@property (nonatomic, strong)nsoperationqueue *myqueue;

@property (nonatomic, strong)uitableview *tableview;

@property (nonatomic, strong)nsmutabledictionary *operations;

@property (nonatomic, strong)nsmutabledictionary *images;

@end

@implementation viewcontroller

static int page = 1;

#pragma mark life cycle

- (void)viewdidload

//傳送網路請求獲取資料

[self loaddata];

}- (void)viewdidlayoutsubviews

#pragma mark tableviewdatasourece

- (nsinteger)numberofsectionsintableview:(uitableview *)tableview

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath

if (self.datasource.count >0)

//先設定一張佔位

cell.imageview.image = [uiimage imagenamed:@"reflesh1_60x55"];

zccurrentoperation *operation = self.operations[liveuser.photo];

//什麼都不做

}else }}

return cell;

}- (void)loaddata

} failure:^(nserror *error) ];

}- (void)downloadoperation:(zccurrentoperation *)operation didfisheddownload:(uiimage *)image

#pragma mark懶載入相關

- (nsoperationqueue *)myqueue

return _myqueue;

}- (uitableview *)tableview

return _tableview;

}- (nsmutablearray *)datasource

return _datasource;

}- (nsmutabledictionary *)operations

return _operations;

}- (nsmutabledictionary *)images

return _images;

}@end

列印結果如下:

以上就是自定義併發nsoperation的乙個簡單應用,功能類似於sdwebimage.

iOS開發 自定義列表

tableview幾乎是ios裡面最常用的布局了,這裡用純 的方式實現了自定義列表cell的介面 自定義cell cardlist created by yxhe on 16 5 17.end customcellview.m cardlist created by yxhe on 16 5 17....

iOS基礎開發 自定義控制項

自定義控制項,設定子控制項的尺寸和位置 當系統提供的控制項滿足不了我們的需求,我們可以自定義乙個控制項,繼承系統自帶的控制項,寫乙個屬於自己的控制項.自定義控制項的好處是可以把封裝控制項內部的細節,不容易被外界隨意修改.如果乙個view內部的子控制項比較多,一般會考慮自定義乙個view,把它內部子控...

IOS開發自定義CheckBox控制項

ios本身沒有系統的checkbox元件,但是實際開發中會經常用到,所以專門寫了乙個checkbox控制項,直接上 效果圖 uicheckboxbutton.h檔案如下 import import common.h inte ce uicheckboxbutton uicontrol uilabel...