1

セル内の検索文字列を強調表示するアプリケーションの検索機能を作成しています。これを行うには、検索文字列を からアクセスできる
グローバル変数に保存します。次に、返されるセルのコンテンツを強調表示します。しかし、それは機能していません。ログを確認すると、reloadData が非同期で実行されているようです (したがって、解放された後)。 このため、私のアプリもクラッシュします.同期または別のアプローチを実行するためのソリューションはありますか? ありがとう!activeSearchStringtableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPathactiveSearchStringactiveSearchString
reloadData

コード:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(){
    // Get the products asynchronous    
    NSString* searchString = [NSString stringWithString:@"A search string"];
    NSArray* searchResults = [[ProductServer sharedServer] productsForSearchString:searchString];
    dispatch_sync(dispatch_get_main_queue(), ^(){
        DLog(@"Begin");
        activeSearchString = [searchString retain];
        products = [searchResults retain];
        [ibTableView reloadData];
        [activeSearchString release];
        DLog(@"End");
    });
});

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        DLog();
        ProductTableViewCell* tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"productCell" forIndexPath:indexPath];
        Product* product = [products objectAtIndex:[indexPath row]];

        NSDictionary* highlightAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:49.f/255.f green:110.f/255.f blue:184.f/255.f alpha:1.f], NSBackgroundColorAttributeName, nil];
        NSMutableAttributedString* mutableAttributedTitle = [[[NSMutableAttributedString alloc] initWithString:[product title]] autorelease];
        [mutableAttributedTitle setAttributes:highlightAttributes range:[[mutableAttributedTitle string] rangeOfString:activeSearchString options:NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch]];
        [(UILabel*)[tableViewCell titleLabel] setAttributedText:mutableAttributedTitle];

        return tableViewCell;
}

ログ:

2012-11-13 14:56:00.783 ****[5810:c07] __58-[TVCurrentlyViewController searchBarSearchButtonClicked:]_block_invoke_2 [Line 190] Begin
2012-11-13 14:56:00.783 ****[5810:c07] __58-[TVCurrentlyViewController searchBarSearchButtonClicked:]_block_invoke_2 [Line 197] End
2012-11-13 14:56:00.783 ****[5810:c07] -[TVCurrentlyViewController tableView:cellForRowAtIndexPath:] [Line 117] 
2012-11-13 14:56:00.786 ****[5810:c07] -[TVCurrentlyViewController tableView:cellForRowAtIndexPath:] [Line 117] 
2012-11-13 14:56:00.787 ****[5810:c07] -[TVCurrentlyViewController tableView:cellForRowAtIndexPath:] [Line 117] 
2012-11-13 14:56:00.789 ****[5810:c07] -[TVCurrentlyViewController tableView:cellForRowAtIndexPath:] [Line 117] 
2012-11-13 14:56:00.790 ****[5810:c07] *** -[CFString length]: message sent to deallocated instance 0x75d6d00
4

1 に答える 1

4

そこでグローバルをリリースするべきではありません。ビュー コントローラの dealloc メソッドで保持セマンティクスと解放を備えた合成プロパティを使用します。

編集:

メインスレッドへのコールバックでdispatch_async()代わりに使用します。メインスレッドがテーブルビューの更新を完了する間、グローバル バックグラウンド キューをブロックします。dispatch_sync()dispatch_sync()

于 2012-11-13T14:38:36.677 に答える