在我的應用程序中,我有一個tableview。每個單元格包含一個UIButton,UIImageView和一個UILabel。當點擊按鈕時,將使用ASINetworkQueue下載一組文件,此時該按鈕將被替換爲UIProgressView。在下載過程中,如果我滾動表格,應用程序崩潰。而我得到的ASIHTTPRequest.m類的錯誤點,UITableViewCell中的UIProgressbar問題
+ (void)updateProgressIndicator:(id *)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total
{
#if TARGET_OS_IPHONE
// Cocoa Touch: UIProgressView
SEL selector = @selector(setProgress:);
float progressAmount = (float)((progress*1.0)/(total*1.0));
#else
// Cocoa: NSProgressIndicator
double progressAmount = progressAmount = (progress*1.0)/(total*1.0);
SEL selector = @selector(setDoubleValue:);
#endif
if (![*indicator respondsToSelector:selector]) { // here i am getting the error
return;
}
[progressLock lock];
[ASIHTTPRequest performSelector:selector onTarget:indicator withObject:nil amount:&progressAmount callerToRetain:nil];
[progressLock unlock];
}
這裏是我的代碼: 我寫了一個UITableViewCell類,像,
@interface UIMenuItemCell : UITableViewCell{
UILabel *cellItemName;
UIImageView *cellitemImage;
UIButton *cellItemButton;
UIProgressView *cellItemProgress;
}
@property (nonatomic, retain) UILabel *cellItemName;
@property (nonatomic, retain) UIImageView *cellitemImage;
@property (nonatomic, retain) UIButton *cellItemButton;
@property (nonatomic, retain) UIProgressView *cellItemProgress;
和
- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 150, 60);
CGRect imgFrame = CGRectMake(20, 48, 110, 123);
CGRect btnFrame = CGRectMake(25, 140, 100, 26);
UIImageView *itemImg;
UIButton *itemBtn;
UIMenuItemCell *cell = [[UIMenuItemCell alloc] init] ;
cell.frame = CellFrame;
//Initialize ImageView
itemImg = [[UIImageView alloc]initWithFrame:imgFrame];
itemImg.tag = 2;
[cell.contentView addSubview:itemImg];
//Initialize Button
itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
itemBtn.frame = btnFrame;
itemBtn.tag = 3;
itemBtn.titleLabel.textColor = [UIColor blueColor];
itemBtn.titleLabel.font = [UIFont systemFontOfSize:9.0];
[cell.contentView addSubview:itemBtn];
return cell;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [self getCellContentView:CellIdentifier];
cell.cellitemImage = (UIImageView *)[cell viewWithTag:2];
cell.cellItemButton = (UIButton *)[cell viewWithTag:3];
DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];
NSString *url;
if ([itemObj.itemStatus isEqualToString:@"NotAvailable"]) {
url = [NSString stringWithFormat:@"%@",itemObj.notAvialableIcon];
[cell.cellItemButton setTitle:date forState:UIControlStateNormal];
cell.cellItemButton.userInteractionEnabled = NO;
cell.userInteractionEnabled = NO;
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"not_available_bttn_bck_img"] forState:UIControlStateNormal];
}else if([itemObj.itemStatus isEqualToString:@"Available"]){
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
[cell.cellItemButton setTitle:@"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"img_pressed"] forState:UIControlStateHighlighted];
[cell.cellItemButton addTarget:self action:@selector(download) forControlEvents:UIControlEventTouchUpInside];
url = [NSString stringWithFormat:@"%@",itemObj.availableIcon];
}
[cell.cellitemImage setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"item01.png"]];
cell.cellItemName.text = [NSString stringWithFormat:@"%@",itemObj.itemName];
return cell;
}
和下載操作:
- (void)download{
//adding custom method to add the uiprogressview into the selected cell
//and setting the progress delegate for the queue
[self.myQueue setDownloadProgressDelegate:currentProgress];
//then starts download
[self.myQueue go];
}
請分享你的想法。 編輯:在這裏,我在同一時間實施多個下載。
不錯的建議,但正如你所說,它需要更多的工作。那麼你可以給任何教程或示例代碼來實現這個? – Mithuzz