0

私のアプリケーションでは、テーブルビューがあります。各セルには、UIButton、UIImageView、および UILabel が含まれています。ボタンをクリックすると、ASINetworkQueue を使用してファイルのグループがダウンロードされます。この時点で、ボタンは UIProgressView に置き換えられます。ダウンロードの進行中にテーブルをスクロールすると、アプリがクラッシュします。そして、ASIHTTPRequest.m クラスでエラー ポイントを取得しています。

+ (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];
}

あなたの考えを共有してください。 編集:ここでは、同時に複数のダウンロードを実装しています。

4

2 に答える 2

1

何が起こっているかというと、セルが画面の外に出ると進行状況バーが解放され、ASI が解放されたオブジェクトを更新しようとします。2 つを互いに分離する必要があります。ダウンローダー クラスを作成して、これを許可することができます。クラスはセルに通知を送信します。これにはあなたの側でいくつかの作業が必要になることに注意してください。頑張ってください:)

于 2012-06-17T20:41:31.930 に答える
1

@Omarは問題について正しいです。ソリューションのスケッチを次に示します。

「downloadProgress」などのプロパティを DatabaseClass に追加します。これは、ダウンロードが行われていないときは値が負になり、ダウンロードが行われている間は 0.0 から 1.0 の間の値になる float にすることができます。

カスタム セルに非表示の進行状況インジケーターを追加します。

ダウンロード ボタンが押されると、対応するデータベース アイテムを取得し、ダウンロードを開始します (アイテムの downloadStatus を 0.0 に設定します)。非同期プロセスから進行状況を取得したら、そのアイテムの downloadStatus を更新します。

これを行うたびに、ステータスが変更されたことをテーブルに知らせます (ここでは NSNotification が適切なメカニズムである可能性があります)。テーブル VC は reloadRowsAtIndexPaths: を呼び出し、データベース アイテムに対応するインデックス パスを含む配列を指定する必要があります。

セルを構成するとき、テーブル VC はアイテムのダウンロード ステータスを確認する必要があります。正の場合は、ステータス インジケーターを再表示し、フロートに従って進行状況を設定します。

また、クラッシュについて: インジケーターが入力されている updateProgress メソッド (id *) に少なくとも 1 つの別の問題があるようです。これは ID へのポインターのようなものであり、ほとんどの場合、必要なものではありません。id のみを使用し、本体のインジケーターを *indicator として参照しないでください。

于 2012-06-17T21:51:28.037 に答える