0

画像の初期化を含むテーブルビューを持つビューコントローラーがあります。AFNetworking で画像を読み込んでいますが、(主にインターネット接続が遅い場合に) View Controller の backbuttonitem をクリックして rootviewcontroller に入ると、アプリがクラッシュし、次のメッセージが表示されることがあります。

-[__NSCFDictionary numberOfSectionsInTableView:]: unrecognized selector sent to instance 0x1f567e30

なんで?ARCを使用しています。画像をロードするための私のコードは次のとおりです

if(item.thumb)
    {
        [cell.listItemImage setImage: item.thumb];

    }
    else
    {

        UIActivityIndicatorView *loadingSymbol = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
        loadingSymbol.frame = CGRectMake(0, 0, cell.listItemImage.frame.size.width, cell.listItemImage.frame.size.height);
        [cell.listItemImage addSubview: loadingSymbol];
        [loadingSymbol startAnimating];


        [cell.listItemImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: item.thumbUrl]]
                                  placeholderImage:[UIImage imageNamed:@"transparent.png"]
                                           success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image )
        {

                item.thumb = image;
            [cell setNeedsLayout];
                [loadingSymbol removeFromSuperview];
               [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

        }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
        {
             NSLog(@"something went wrong wiv the images");
            NSLog(@"%@", error);
             //[loadingSymbol removeFromSuperview];

        }
         ];
    }
}

誰かがここで私を助けてくれたら本当に嬉しいです!

編集:

私の最初の問題を解決しました。しかし、テーブル ビューでスクロールするとアプリがクラッシュします。次の行と関係があると思います。

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

理由はありますか?

4

2 に答える 2

0

問題: View Controller の割り当てが解除された後、しばらくしてネットワーク I/O が終了します。ネットワーク I/O の結果として、いくつかのセルをリロードします。セルをリロードすると、dataSource 情報とクラッシュのために View Controller (割り当て解除) が呼び出されます。

解決策: テーブル ビュー デリゲートと dataSource を dealloc で nil アウトする必要があります。

- (void)dealloc {
    // Cleanup table view 
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;

    // Stop network operations since their are not needed anymore.
    for (UITableViewCell *cell in [self.tableView visibleCells]) {
        [cell. listItemImage cancelImageRequestOperation];
    }
 }

cellForRowAtIndexPath で、再利用する前に画像のダウンロードをキャンセルします。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MyCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // Cancel network request
    [cell. listItemImage cancelImageRequestOperation];
    ...
}
于 2013-06-02T19:07:48.003 に答える