Я пытаюсь обновить значение прогресса загрузки нескольких файлов до UIProgressView в ячейке таблицы. У меня есть класс FileDownloader с NSOperationQueue, который выполняет асинхронные операции загрузки. Я думаю обновить пользовательский интерфейс, используя «делегат» из класса FileDownloader. Но я не могу скомпилировать коды. У меня есть FileDownloader как Singleton. Мне интересно, не упускаю ли я что-то фундаментальное.
Далее идет настройка кода.
FileDownloader.h
#import < Foundation/Foundation.h >
// declare protocol to accept progress status of file downloads
@protocol FileDownloaderDelegate < NSObject >
// this function will update the progressview and re-display the cell
- (void) updateProgessWithCurrentValue:(NSNumber*)value totalValue:(NSNumber*)totalValue;
@end
@interface FileDownloader : NSObject {
NSOperationQueue *operationQueue; // for file download operations
id < FileDownloaderDelegate > delegate; // to send progess value to UI
}
@property (nonatomic, retain) NSOperationQueue *operationQueue;
@property (nonatomic, assign) id < FileDownloaderDelegate > delegate;
+(FileDownloader*) sharedInstance; // FileDownloader is Singleton with its standard methods
// when download is progressing, the delegate function will be called like
// [self.delegate updateProgessWithCurrentValue:10 totalValue:100];
//
@end
MyTableViewCell.h
#import < UIKit/UIKit.h >
#import < FileDownloader.h >
@interface MyTableViewCell : UITableViewCell < FileDownloaderDelegate > {
UIProgressView *progressView;
}
@property (nonatomic, retain) UIProgressView *progressView;
// MyTableViewCell.m will have the implementation of
// - (void) updateProgessWithCurrentValue:(NSNumber*)value totalValue:(NSNumber*)totalValue;
// to update UI
@end
Во-первых, я получил ошибку компилятора "не могу найти объявление протокола для FileDownloaderDelegate" в MyTableViewCell. Поэтому я переместил объявление протокола FileDownloaderDelegate в отдельный файл .h, чтобы его можно было скомпилировать. Даже тогда я все еще не могу назначить делегата из моего tableViewController, используя
[[Общий экземпляр FileDownloader] setDelegate:myTableViewCell];
Я получил предупреждение «FileDownloader может не отвечать на метод setDelegate», что означает, что он не знает делегата (хотя у меня есть делегат @synthesize). Мне интересно, не понимаю ли я что-то об использовании синглтона или делегата.