IOS 开发上传管理器
由于项目需要整合多处的上传功能,涉及到的主要有数据库(FMDB),多线程()
1、新建项目,下载依赖库
首先新建一个项目这里命名为UploadManager,项目依赖库采用CocoaPods来管理所以在终端进入UploadManager项目中,输入
pod init
这时会看到项目中多了一个文件Podfile,然后打开它,其内容如下:# Uncomment this line to define a global platform for your project
# platform :ios, ‘6.0‘
target ‘UploadManager‘ do
end
target ‘UploadManagerTests‘ do
end
然后添加FMDB信息,如下:
# Uncomment this line to define a global platform for your project
# platform :ios, ‘6.0‘
target ‘UploadManager‘ do
pod‘FMDB‘,‘2.5‘
end
target ‘UploadManagerTests‘ do
end
然后关闭项目,回到终端,输入
pod install
会自动分析依赖库,并下载下来,等待一段时间后,终端出现如下内容,就说明下载完毕
Analyzing dependencies CocoaPods 0.37.1 is available. To update use: `gem install cocoapods` For more information see http://blog.cocoapods.org and the CHANGELOG for this version http://git.io/BaH8pQ. Downloading dependencies Installing FMDB (2.5) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `UploadManager.xcworkspace` for this project from now on.
然后打开项目目录,会看到项目中多了一些内容,打开其中的UploadManager.xcworkspace
2、多线程处理
项目中采用NSOperationQueue和NSOperation来实现多线程操作和管理,设计PendingOperations类来保存上传任务队列、完成任务队列以及线程队列,MerchantTask类自定义NSOperation来实际完成任务,其中添加一个协议MerchantTaskDelegate来实现更新UI界面的进度,代码如下:// // PendingOperations.h // UploadManager // // Created by Soheil M. Azarpour on 8/11/12. // Copyright (c) 2012 iOS Developer. All rights reserved. // #import <Foundation/Foundation.h> @interface PendingOperations : NSObject @property (nonatomic, strong) NSMutableDictionary *uploadInProgress; @property (nonatomic, strong) NSMutableDictionary *uploadFinishProgress; @property (nonatomic, strong) NSOperationQueue *uploadQueue; @end
// // PendingOperations.m // UploadManager // // Created by Soheil M. Azarpour on 8/11/12. // Copyright (c) 2012 iOS Developer. All rights reserved. // #import "PendingOperations.h" @implementation PendingOperations @synthesize uploadInProgress = _uploadInProgress; @synthesize uploadFinishProgress = _uploadFinishProgress; @synthesize uploadQueue = _uploadQueue; - (NSMutableDictionary *)uploadInProgress { if (!_uploadInProgress) { _uploadInProgress = [[NSMutableDictionary alloc] init]; } return _uploadInProgress; } - (NSOperationQueue *)uploadQueue { if (!_uploadQueue) { _uploadQueue = [[NSOperationQueue alloc] init]; _uploadQueue.name = @"Upload Queue"; _uploadQueue.maxConcurrentOperationCount = 4; } return _uploadQueue; } @end
// // MerchantTask.h // UploadManager // // Created by 张杰 on 15/5/8. // Copyright (c) 2015年 张杰. All rights reserved. // #import <Foundation/Foundation.h> #import "UploadTable.h" @protocol MerchantTaskDelegate; @interface MerchantTask : NSOperation @property (nonatomic, assign) id <MerchantTaskDelegate> delegate; @property (nonatomic, readonly, strong) NSIndexPath *indexPathInTableView; @property (nonatomic, readonly, strong) UploadTable *taskRecord; - (id)initWithTaskRecord:(UploadTable *)record atIndexPath:(NSIndexPath *)indexPath delegate:(id<MerchantTaskDelegate>) theDelegate; -(NSString *) uploadText : (NSString*)textUrl withText:(NSString*)text; -(NSString *) uploadPic : (NSString *)picUrl widthPicText:(NSString*)picText; -(NSString *) sysPicInfo : (NSString *)sysUrl withSysText:(NSString*) sysText; @end @protocol MerchantTaskDelegate <NSObject> // 5: In your delegate method, pass the whole class as an object back to the caller so that the caller can access both indexPathInTableView and photoRecord. Because you need to cast the operation to NSObject and return it on the main thread, the delegate method canít have more than one argument. - (void) merchantTaskDidFinish:(MerchantTask *)uploader; @end在ViewController中实现MerchantTaskDelegate这个协议,来实现在线程中更新UI
// // MerchantTask.m // UploadManager // Created by 张杰 on 15/5/8. // Copyright (c) 2015年 张杰. All rights reserved. // #import "MerchantTask.h" @interface MerchantTask() @property (nonatomic, readwrite, strong) NSIndexPath *indexPathInTableView; @property (nonatomic, readwrite, strong) UploadTable *taskRecord; @end @implementation MerchantTask @synthesize delegate = _delegate; @synthesize indexPathInTableView = _indexPathInTableView; @synthesize taskRecord = _taskRecord; #pragma mark - Life Cycle - (id)initWithTaskRecord:(UploadTable *)record atIndexPath:(NSIndexPath *)indexPath delegate:(id<MerchantTaskDelegate>) theDelegate { if (self = [super init]) { // 2: Set the properties. self.delegate = theDelegate; self.indexPathInTableView = indexPath; _taskRecord = record; } return self; } -(void) main{ @autoreleasepool { _taskRecord.starting = TRUE; [self uploadText:@"http://www.ssd" withText:@"dadfasdfad"]; } } //未处理:0;文字上传中:1;文字上传失败:2;图片未上传:3;图片上传中:4;图片上传失败:5;图片同步中:6;图片同步失败:7 -(NSString *)uploadText:(NSString *)textUrl withText:(NSString *)text{ _taskRecord.upload_progress = 1; _taskRecord.upload_status = 0; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; NSLog(@"开始上传文字"); usleep(1000*1000); [self uploadPic:@"http://www.pic" widthPicText:@"上传图片"]; return @""; } -(NSString *)uploadPic:(NSString *)picUrl widthPicText:(NSString *)picText{ _taskRecord.upload_progress = 3; _taskRecord.upload_status = 1; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; NSLog(@"开始上传图片"); usleep(1000*1000); _taskRecord.upload_progress = -1; _taskRecord.upload_status = 1; _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 10, @"%"]; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; usleep(1000*2000); _taskRecord.upload_progress = -1; _taskRecord.upload_status = 1; _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 30, @"%"]; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; usleep(1000*3000); _taskRecord.upload_progress = -1; _taskRecord.upload_status = 1; _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 70, @"%"]; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; usleep(1000*4000); _taskRecord.upload_progress = -1; _taskRecord.upload_status = 1; _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 100, @"%"]; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; usleep(1000*5000); _taskRecord.upload_progress = -1; _taskRecord.upload_status = 2; _taskRecord.upload_show_text = [NSString stringWithFormat:@"%d %@", 100, @"%"]; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; usleep(1000*5000); [self sysPicInfo:@"" withSysText:@""]; return @""; } -(NSString *)sysPicInfo:(NSString *)sysUrl withSysText:(NSString *)sysText{ _taskRecord.upload_progress = 6; _taskRecord.upload_status = 2; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; NSLog(@"开始同步图片"); usleep(1000*5000); _taskRecord.upload_progress = 8; _taskRecord.upload_status = 3; [(NSObject *)self.delegate performSelectorOnMainThread:@selector(merchantTaskDidFinish:) withObject:self waitUntilDone:NO]; return @""; } @end上面的数据是用来测试的。
3、数据库设计
首先是表结构设计,如下:
// // UploadTable.h // UploadManager // // Created by 张杰 on 15/5/8. // Copyright (c) 2015年 张杰. All rights reserved. // #import <Foundation/Foundation.h> @interface UploadTable : NSObject @property (nonatomic, strong) NSString *upload_id;//协议、工单、临时工单的ID @property (nonatomic, strong) NSString *upload_name;//协议、工单、临时工单的商户名称 @property (nonatomic, strong) NSString *upload_type;//协议:0;工单:1;临时工单:2 @property (nonatomic, strong) NSString *upload_text;//文字信息格式json @property (nonatomic, strong) NSString *upload_img_name;//图片的名字格式json @property (nonatomic, strong) NSString *upload_img_sys;//图片同步数据 @property int upload_progress;//UI的同步状态 上传进度:-1;未处理:0;文字上传中:1;文字上传失败:2;图片未上传:3;图片上传中:4;图片上传失败:5;图片同步中:6;图片同步失败:7;上传完成8 @property int upload_status;//线程中使用 0:未处理 1:文字已上传 2:图片已上传 3:同步成功 @property (nonatomic, strong) NSString *upload_show_text;//上传显示文字的进度 @property (nonatomic, getter = isStarting) BOOL starting; // 任务是否正在进行 @end
// // UploadTable.m // UploadManager // // Created by 张杰 on 15/5/8. // Copyright (c) 2015年 张杰. All rights reserved. // #import "UploadTable.h" @implementation UploadTable @synthesize upload_id = _upload_id; @synthesize upload_name = _upload_name; @synthesize upload_type = _upload_type; @synthesize upload_text = _upload_text; @synthesize upload_img_name = _upload_img_name; @synthesize upload_img_sys = _upload_img_sys; @synthesize upload_progress = _upload_progress; @synthesize upload_status = _upload_status; @synthesize upload_show_text = _upload_show_text; @synthesize starting = _starting; -(BOOL)isStarting{ return _starting; } @end
数据库操作:
// // DBHelper.h // UploadManager // // Created by 张杰 on 15/5/8. // Copyright (c) 2015年 张杰. All rights reserved. // #import <Foundation/Foundation.h> #import "FMDatabase.h" #import "DBHelperOperation.h" @interface DBHelper : NSObject <DBHelperOperation> +(DBHelper*) getInstance; @end
// // DBHelper.m // UploadManager // 创建数据库,表结构 // Created by 张杰 on 15/5/8. // Copyright (c) 2015年 张杰. All rights reserved. // #import "DBHelper.h" @interface DBHelper(){ FMDatabase *db; //数据库表结构字段 NSString *TABLE_NAME;//表名字 NSString *UPLOAD_ID;//协议、工单、临时工单的ID NSString *UPLOAD_NAME;//协议、工单、临时工单的商户名称 NSString *UPLOAD_TYPE;//协议:0;工单:1;临时工单:2 NSString *UPLOAD_TEXT;//文字信息格式json NSString *UPLOAD_IMG_NAME;//图片的名字格式json NSString *UPLOAD_IMG_SYS;//图片同步数据 NSString *UPLOAD_PROGRESS;//UI的同步状态 未处理:0;文字上传中:1;文字上传失败:2;图片未上传:3;图片上传中:4;图片上传失败:5;图片同步中:6;图片同步失败:7 NSString *UPLOAD_STATUS;//线程中使用 0:未处理 1:文字已更新 2:文字已上传 3:图片已上传 4:同步成功 } @end @implementation DBHelper +(DBHelper*) getInstance { static DBHelper *instance = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ instance = [[self alloc] init]; [instance onCreate]; [instance onCreateTable]; NSLog(@"init"); }); return instance; } - (instancetype)init { self = [super init]; if (self) { TABLE_NAME = @"table_upload_status"; UPLOAD_ID = @"upload_id"; UPLOAD_NAME = @"upload_name"; UPLOAD_TYPE = @"upload_type"; UPLOAD_TEXT = @"upload_text"; UPLOAD_IMG_NAME = @"upload_img_name"; UPLOAD_IMG_SYS = @"upload_img_sys"; UPLOAD_PROGRESS = @"upload_progress"; UPLOAD_STATUS = @"upload_status"; } return self; } -(void)onCreate{ NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *dbPath = [docsPath stringByAppendingPathComponent:@"umpad_upload_manager.db"]; db = [FMDatabase databaseWithPath:dbPath]; } -(BOOL)onCreateTable{ if ([db open]) { NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS ‘%@‘ (id INTEGER PRIMARY KEY AUTOINCREMENT, ‘%@‘ TEXT, ‘%@‘ TEXT, ‘%@‘ TEXT, ‘%@‘ TEXT, ‘%@‘ TEXT, ‘%@‘ TEXT, ‘%@‘ INTEGER, ‘%@‘ INTEGER)", TABLE_NAME, UPLOAD_ID, UPLOAD_NAME, UPLOAD_TYPE, UPLOAD_TEXT, UPLOAD_IMG_NAME, UPLOAD_IMG_SYS, UPLOAD_PROGRESS, UPLOAD_STATUS]; [db beginTransaction]; BOOL res = [db executeUpdate: sqlCreateTable]; if (res) { NSLog(@"创建表成功"); }else{ NSLog(@"创建表失败"); } [db commit]; [db close]; return res; } return FALSE; } -(BOOL)onUpgrade{ return FALSE; } //插入一条数据 -(BOOL) insertOneData : (UploadTable*) table{ if ([db open]) { NSString *insertSql= [NSString stringWithFormat: @"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘) VALUES (‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%d‘, ‘%d‘)", TABLE_NAME, UPLOAD_ID, UPLOAD_NAME, UPLOAD_TYPE, UPLOAD_TEXT, UPLOAD_IMG_NAME, UPLOAD_IMG_SYS, UPLOAD_PROGRESS, UPLOAD_STATUS, table.upload_id, table.upload_name, table.upload_type, table.upload_text, table.upload_img_name, table.upload_img_sys, table.upload_progress, table.upload_status]; [db beginTransaction]; BOOL res = [db executeUpdate:insertSql]; if (res) { NSLog(@"insertOneData成功"); }else{ NSLog(@"insertOneData失败"); } [db commit]; [db close]; return res; } return FALSE; } //更新一条数据 -(BOOL) uploadOneData : (UploadTable*) table{ if ([db open]) { NSString *updateSql = [NSString stringWithFormat:@"UPDATE ‘%@‘ SET ‘%@‘ = ‘%@‘, ‘%@‘ = ‘%@‘, ‘%@‘ = ‘%@‘, ‘%@‘ = ‘%@‘,‘%@‘ = ‘%d‘, ‘%@‘ = ‘%d‘, WHERE ‘%@‘ = ‘%@‘", TABLE_NAME, UPLOAD_NAME, table.upload_name, UPLOAD_TEXT, table.upload_text, UPLOAD_IMG_NAME, table.upload_img_name, UPLOAD_IMG_SYS, table.upload_img_sys, UPLOAD_PROGRESS, table.upload_progress, UPLOAD_STATUS, table.upload_status, UPLOAD_ID, table.upload_id]; [db beginTransaction]; BOOL res = [db executeUpdate:updateSql]; [db commit]; [db close]; return res; } return FALSE; } //删除一条数据 -(BOOL) deleteOneData : (NSString*) upload_id{ if ([db open]) { NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM ‘%@‘ WHERE ‘%@‘ = ‘%@‘", TABLE_NAME, UPLOAD_ID, upload_id]; [db beginTransaction]; BOOL res = [db executeUpdate:deleteSql]; [db commit]; [db close]; return res; } return FALSE; } //删除所有数据 -(BOOL) deleteAllData{ if ([db open]) { NSString *deleteAllSql = [NSString stringWithFormat:@"DELETE FROM ‘%@‘", TABLE_NAME]; [db beginTransaction]; BOOL res = [db executeUpdate:deleteAllSql]; [db commit]; [db close]; return res; } return FALSE; } //判断当前数据是否存在 -(BOOL) isOneDataExist : (NSString*) upload_id{ return FALSE; } //修改线程使用状态 -(BOOL) modifyUploadStatus : (NSString*) upload_id andStatus : (int)status{ if ([db open]) { NSString *modifyStatus = [NSString stringWithFormat:@"UPDATE ‘%@‘ SET ‘%@‘ = ‘%d‘ WHERE ‘%@‘ = ‘%@‘", TABLE_NAME, UPLOAD_STATUS, status, UPLOAD_ID, upload_id]; [db beginTransaction]; BOOL res = [db executeUpdate:modifyStatus]; [db commit]; [db close]; return res; } return FALSE; } //修改UI使用状态 -(BOOL) modifyUploadProgressStatus : (NSString *)upload_id andStatus : (int)progressStatus{ if ([db open]) { NSString *modifyProgressStatus = [NSString stringWithFormat:@"UPDATE ‘%@‘ SET ‘%@‘ = ‘%d‘ WHERE ‘%@‘ = ‘%@‘", TABLE_NAME, UPLOAD_PROGRESS, progressStatus, UPLOAD_ID, upload_id]; [db beginTransaction]; BOOL res = [db executeUpdate:modifyProgressStatus]; [db commit]; [db close]; return res; } return FALSE; } //修改两个状态 -(BOOL) modifyStatusAll : (NSString*)upload_id andStatus : (int)status andProgressStatus : (int)progressStatus{ if ([db open]) { NSString *modifyStatusAll = [NSString stringWithFormat:@"UPDATE ‘%@‘ SET ‘%@‘ = ‘%d‘, ‘%@‘ = ‘%d‘ WHERE ‘%@‘ = ‘%@‘", TABLE_NAME, UPLOAD_PROGRESS, progressStatus, UPLOAD_STATUS, status, UPLOAD_ID, upload_id]; [db beginTransaction]; BOOL res = [db executeUpdate:modifyStatusAll]; [db commit]; [db close]; return res; } return FALSE; } //查询一条数据 -(UploadTable*) queryDataByUploadId : (NSString*)upload_id{ if ([db open]) { NSString *queryOneSql = [NSString stringWithFormat:@"SELECT * FROM ‘%@‘ WHERE ‘%@‘ = ‘%@‘", TABLE_NAME, UPLOAD_ID, upload_id]; FMResultSet *resultSet = [db executeQuery:queryOneSql]; if ([resultSet next]) { UploadTable *result = [[UploadTable alloc]init]; result.upload_id = [resultSet stringForColumn:UPLOAD_ID]; result.upload_name = [resultSet stringForColumn:UPLOAD_NAME]; result.upload_text = [resultSet stringForColumn:UPLOAD_TEXT]; result.upload_type = [resultSet stringForColumn:UPLOAD_TYPE]; result.upload_img_name = [resultSet stringForColumn:UPLOAD_IMG_NAME]; result.upload_img_sys = [resultSet stringForColumn:UPLOAD_IMG_SYS]; result.upload_progress = [resultSet intForColumn:UPLOAD_PROGRESS]; result.upload_status = [resultSet intForColumn:UPLOAD_STATUS]; return result; } [db close]; } return nil; } //查询所有的数据 -(NSMutableArray*) queryDataAll{ NSMutableArray *restArray = [[NSMutableArray alloc] init]; if ([db open]) { NSString *queryAllSql = [NSString stringWithFormat:@"SELECT * FROM ‘%@‘", TABLE_NAME]; FMResultSet *resultSet = [db executeQuery:queryAllSql]; while ([resultSet next]) { UploadTable *result = [[UploadTable alloc]init]; result.upload_id = [resultSet stringForColumn:UPLOAD_ID]; result.upload_name = [resultSet stringForColumn:UPLOAD_NAME]; result.upload_text = [resultSet stringForColumn:UPLOAD_TEXT]; result.upload_type = [resultSet stringForColumn:UPLOAD_TYPE]; result.upload_img_name = [resultSet stringForColumn:UPLOAD_IMG_NAME]; result.upload_img_sys = [resultSet stringForColumn:UPLOAD_IMG_SYS]; result.upload_progress = [resultSet intForColumn:UPLOAD_PROGRESS]; result.upload_status = [resultSet intForColumn:UPLOAD_STATUS]; [restArray addObject:(result)]; } [db close]; } return restArray; } @end
在DBHelper中实现了增删改查功能,里面的方法是在协议DBHelperOperation中:
// // DBHelperOperation.h // UploadManager // // Created by 张杰 on 15/5/8. // Copyright (c) 2015年 张杰. All rights reserved. // #import "UploadTable.h" @protocol DBHelperOperation <NSObject> //创建数据 -(void)onCreate; //创建表 -(BOOL)onCreateTable; //更新数据库 -(BOOL)onUpgrade; //插入一条数据 -(BOOL) insertOneData : (UploadTable*) table; //更新一条数据 -(BOOL) uploadOneData : (UploadTable*) table; //删除一条数据 -(BOOL) deleteOneData : (NSString*) table; //删除所有数据 -(BOOL) deleteAllData; //判断当前数据是否存在 -(BOOL) isOneDataExist : (NSString*) upload_id; //修改线程使用状态 -(BOOL) modifyUploadStatus : (NSString*) upload_id andStatus : (int)status; //修改UI使用状态 -(BOOL) modifyUploadProgressStatus : (NSString *)upload_id andStatus : (int)progressStatus; //修改两个状态 -(BOOL) modifyStatusAll : (NSString*)upload_id andStatus : (int)status andProgressStatus : (int)progressStatus; //查询一条数据 -(UploadTable*) queryDataByUploadId : (NSString*)upload_id; //查询所有的数据 -(NSMutableArray*) queryDataAll; @end
4、UI界面操作
// // ViewController.h // UploadManager // // Created by 张杰 on 15/5/14. // Copyright (c) 2015年 张杰. All rights reserved. // #import <UIKit/UIKit.h> #import "DBHelper.h" #import "PendingOperations.h" #import "MerchantTask.h" #import "UploadTable.h" #import "UploadTableCell.h" @interface ViewController : UITableViewController <MerchantTaskDelegate> @property (nonatomic, strong) NSMutableArray *tableData; // main data source of controller @property (nonatomic, strong) PendingOperations *pendingOperations; @end
// // ViewController.m // UploadManager // // Created by 张杰 on 15/5/14. // Copyright (c) 2015年 张杰. All rights reserved. // #import "ViewController.h" @interface ViewController(){ DBHelper *dbHelper; } @end @implementation ViewController @synthesize tableData = _tableData; @synthesize pendingOperations = _pendingOperations; - (PendingOperations *)pendingOperations { if (!_pendingOperations) { _pendingOperations = [[PendingOperations alloc] init]; } return _pendingOperations; } - (void)viewDidLoad { [super viewDidLoad]; dbHelper = [DBHelper getInstance]; // [dbHelper deleteAllData]; // UploadTable *table1 = [[UploadTable alloc] init]; // table1.upload_id = @"lskjfsfjsfa"; // table1.upload_name = @"上海科技有限技术责任公司"; // table1.upload_type = @"协议"; // table1.upload_progress = 70; // [dbHelper insertOneData:table1]; // UploadTable *table2 = [[UploadTable alloc] init]; // table2.upload_id = @"33lk333"; // table2.upload_name = @"上海榕湖全资子公司回执信息办事处"; // table2.upload_type = @"工单"; // table2.upload_progress = 48; // [dbHelper insertOneData:table2]; _tableData = [dbHelper queryDataAll]; } - (void)viewDidUnload { [super viewDidUnload]; } - (void)didReceiveMemoryWarning { [self cancelAllOperations]; [super didReceiveMemoryWarning]; } //返回某个节中的行数 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_tableData count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"uploadTableCell"; UploadTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UploadTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } UploadTable *uploadTable = [_tableData objectAtIndex:indexPath.row]; // 未处理:0;文字上传中:1;文字上传失败:2;图片未上传:3;图片上传中:4;图片上传失败:5;图片同步中:6;图片同步失败:7;图片上传进度:8 switch (uploadTable.upload_progress) { case 0: cell.uploadProgress.text = @"未处理"; break; case 1: cell.uploadProgress.text = @"文字上传中"; break; case 2: cell.uploadProgress.text = @"文字上传失败"; break; case 3: cell.uploadProgress.text = @"图片未上传"; break; case 4: cell.uploadProgress.text = @"图片上传中"; break; case 5: cell.uploadProgress.text = @"图片上传失败"; break; case 6: cell.uploadProgress.text = @"图片同步中"; break; case 7: cell.uploadProgress.text = @"图片同步失败"; break; case 8: cell.uploadProgress.text = @"上传完成"; break; case -1: cell.uploadProgress.text = uploadTable.upload_show_text; break; default: break; } cell.dataName.text = uploadTable.upload_name; cell.dataType.text = uploadTable.upload_type; cell.upload_id = uploadTable.upload_id; [self startOperationsForTaskRecord:uploadTable atIndexPath:indexPath]; return cell; } #pragma mark - #pragma mark - Operations - (void)startOperationsForTaskRecord:(UploadTable *)record atIndexPath:(NSIndexPath *)indexPath { if (!record.isStarting) { [self startTaskingForRecord:record atIndexPath:indexPath]; } } - (void)startTaskingForRecord:(UploadTable *)record atIndexPath:(NSIndexPath *)indexPath { if (![self.pendingOperations.uploadInProgress.allKeys containsObject:indexPath]) { MerchantTask *merchantTask = [[MerchantTask alloc] initWithTaskRecord:record atIndexPath:indexPath delegate:self]; [self.pendingOperations.uploadInProgress setObject:merchantTask forKey:indexPath]; [self.pendingOperations.uploadQueue addOperation:merchantTask]; } } #pragma mark - #pragma mark - Cancelling, suspending, resuming queues / operations - (void)suspendAllOperations { [self.pendingOperations.uploadQueue setSuspended:YES]; } - (void)resumeAllOperations { [self.pendingOperations.uploadQueue setSuspended:NO]; } - (void)cancelAllOperations { [self.pendingOperations.uploadQueue cancelAllOperations]; } - (IBAction)upLoadBtn:(id)sender { } - (void) merchantTaskDidFinish:(MerchantTask *)uploader{ // 1: Check for the indexPath of the operation, whether it is a download, or filtration. NSIndexPath *indexPath = uploader.indexPathInTableView; NSLog(@"upload_id: %@ ; upload_progress: %d",uploader.taskRecord.upload_name, uploader.taskRecord.upload_progress); // 2: Get hold of the PhotoRecord instance. UploadTable *theRecord = uploader.taskRecord; // 3: Replace the updated PhotoRecord in the main data source (Photos array). [_tableData replaceObjectAtIndex:indexPath.row withObject:theRecord]; // 4: Update UI. [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; if(uploader.taskRecord.upload_status == 3){//上传成功 [self.pendingOperations.uploadInProgress removeObjectForKey:indexPath]; [self.pendingOperations.uploadFinishProgress setObject:uploader forKey:indexPath]; } } @end
5、实现效果:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。