2

UITableViewCell があり、その中に UIButton があります。ボタンを押すたびに、ネットワーク呼び出しが行われ、ラベルが更新されます (カウントが増減します)。これは、Facebook の「いいね」の概念に似ています。

問題は、ユーザーが UIButton を繰り返し押すと、値が増減し続けることです。userInteraction を切り替えて、setEnabled 状態を設定しようとしました。まだ動作しません。

次に、このリンクが示唆するようにブロックを使用してみました。まだ動作していません。私はブロックにかなり慣れていません。ここで私が間違っていることはありますか?

これはブロックなしの実装です:

    - (void) giveKarmaCheck:(BOOL)complete // network delegate after the update
    {
        NSLog(@"Completed!");
        _karmaBeingGiven = NO;
        NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section]];
        int qKarma = [[tempDict objectForKey:@"qkarma"] integerValue];
        NSString *karmaCountString;
        QXTHomeCell *questionCell = (QXTHomeCell*)[self.questionsTable cellForRowAtIndexPath:_indexPath];
        if (_karmaGiven)
        {
            [tempDict setValue:[NSNumber numberWithInt:1] forKey:@"qkarmaStatus"];
            qKarma++;
            [questionCell.karmaLogo setImage:[UIImage imageNamed:@"logo-black"]];
            questionCell.karmaCount = [NSNumber numberWithInt:questionCell.karmaCount.integerValue + 1];

            // Question Owner Karma
            NSMutableString *karmaLabelText = [[NSMutableString alloc] initWithString:@"Karma\n"];
            karmaCountString = [NSString stringWithFormat:@"%d", [[[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] objectForKey:@"qownerkarma"]integerValue]+1];

            [karmaLabelText appendString:karmaCountString];
            questionCell.karmaLabel.text = karmaLabelText;
        }
        else
        {
            [tempDict setValue:[NSNumber numberWithInt:0] forKey:@"qkarmaStatus"];
            qKarma--;
            [questionCell.karmaLogo setImage:[UIImage imageNamed:@"logo-grey"]];
            questionCell.karmaCount = [NSNumber numberWithInt:questionCell.karmaCount.integerValue - 1];

            // Question Owner Karma
            NSMutableString *karmaLabelText = [[NSMutableString alloc] initWithString:@"Karma\n"];
            karmaCountString = [NSString stringWithFormat:@"%d", [[[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] objectForKey:@"qownerkarma"]integerValue]-1];

            [karmaLabelText appendString:karmaCountString];
            questionCell.karmaLabel.text = karmaLabelText;
        }
        NSLog(@"Count *** %@", karmaCountString);

        NSMutableArray *tempAnswerArray = [[NSMutableArray alloc] initWithArray:[_questions objectForKey:@"questions"]];
        NSMutableDictionary *tempAnswerDict = [[NSMutableDictionary alloc] initWithDictionary:[tempAnswerArray objectAtIndex:_indexPath.section]];

        [tempAnswerDict setValue:karmaCountString forKey:@"qownerkarma"];
        [tempAnswerArray replaceObjectAtIndex:_indexPath.section withObject:tempAnswerDict];
    //    NSLog(@"Array %@", tempAnswerDict);
        [_questions setObject:tempAnswerArray forKey:@"questions"];

        NSLog(@"Total %@ Count %d", [[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section], [[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] count]);



        [tempDict setValue:[NSNumber numberWithInt:qKarma] forKey:@"qkarma"];
        [[_questions mutableArrayValueForKey:@"questions"] replaceObjectAtIndex:_indexPath.section withObject:tempDict];

        NSMutableString *withoutCount = [[NSMutableString alloc] initWithString:[QXTUtility removeLastWord:questionCell.karmaButton.titleLabel.text]];
        [withoutCount appendString:[NSString stringWithFormat:@" %d", questionCell.karmaCount.integerValue]];
        [questionCell.karmaButton setTitle:[QXTUtility stripDoubleSpaceFrom:withoutCount] forState:UIControlStateNormal];

        [questionCell.karmaButton setUserInteractionEnabled:YES];

    }

ブロックの実装でも同じことをしました:

- (void) giveKarmaCheck:(BOOL)complete
{
    QXTHomeCell *questionCell = (QXTHomeCell*)[self.questionsTable cellForRowAtIndexPath:_indexPath];
    [questionCell.karmaButton setEnabled:NO];

    __block NSMutableString *karmaLabelText, *withoutCount;

    dispatch_async(dispatch_get_global_queue(0,0), ^{
        NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section]];
        int qKarma = [[tempDict objectForKey:@"qkarma"] integerValue];
        NSString *karmaCountString;
        if (_karmaGiven)
        {
            [tempDict setValue:[NSNumber numberWithInt:1] forKey:@"qkarmaStatus"];
            qKarma++;

            // Question Owner Karma
            karmaLabelText = [[NSMutableString alloc] initWithString:@"Karma\n"];
            karmaCountString = [NSString stringWithFormat:@"%d", [[[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] objectForKey:@"qownerkarma"]integerValue]+1];

            [karmaLabelText appendString:karmaCountString];

        }
        else
        {
            [tempDict setValue:[NSNumber numberWithInt:0] forKey:@"qkarmaStatus"];
            qKarma--;

            // Question Owner Karma
            NSMutableString *karmaLabelText = [[NSMutableString alloc] initWithString:@"Karma\n"];
            karmaCountString = [NSString stringWithFormat:@"%d", [[[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] objectForKey:@"qownerkarma"]integerValue]-1];

            [karmaLabelText appendString:karmaCountString];
        }

        NSMutableArray *tempAnswerArray = [[NSMutableArray alloc] initWithArray:[_questions objectForKey:@"questions"]];
        NSMutableDictionary *tempAnswerDict = [[NSMutableDictionary alloc] initWithDictionary:[tempAnswerArray objectAtIndex:_indexPath.section]];

        [tempAnswerDict setValue:karmaCountString forKey:@"qownerkarma"];
        [tempAnswerArray replaceObjectAtIndex:_indexPath.section withObject:tempAnswerDict];
        //    NSLog(@"Array %@", tempAnswerDict);
        [_questions setObject:tempAnswerArray forKey:@"questions"];

        NSLog(@"Total %@ Count %d", [[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section], [[[_questions objectForKey:@"questions"] objectAtIndex:_indexPath.section] count]);



        [tempDict setValue:[NSNumber numberWithInt:qKarma] forKey:@"qkarma"];
        [[_questions mutableArrayValueForKey:@"questions"] replaceObjectAtIndex:_indexPath.section withObject:tempDict];

        withoutCount = [[NSMutableString alloc] initWithString:[QXTUtility removeLastWord:questionCell.karmaButton.titleLabel.text]];
        [withoutCount appendString:[NSString stringWithFormat:@" %d", questionCell.karmaCount.integerValue]];

        dispatch_async(dispatch_get_main_queue(), ^{
            [questionCell.karmaButton setEnabled:YES];
            if (_karmaGiven)
            {
                [questionCell.karmaLogo setImage:[UIImage imageNamed:@"logo-black"]];
                questionCell.karmaCount = [NSNumber numberWithInt:questionCell.karmaCount.integerValue + 1];
                questionCell.karmaLabel.text = karmaLabelText;
            }
            else
            {
                [questionCell.karmaLogo setImage:[UIImage imageNamed:@"logo-grey"]];
                questionCell.karmaCount = [NSNumber numberWithInt:questionCell.karmaCount.integerValue - 1];

                questionCell.karmaLabel.text = karmaLabelText;
            }
            [questionCell.karmaButton setTitle:[QXTUtility stripDoubleSpaceFrom:withoutCount] forState:UIControlStateNormal];
        });


    });

    NSLog(@"Completed!");

}
4

1 に答える 1

0

ボタンの無効化が機能しない理由は、メイン スレッドで実行する必要がある UIKit オブジェクトへの呼び出しであるためです。基本的に、ネットワーク デリゲート メソッドがどのスレッドから呼び出されているかはわかりません (おそらくバックグラウンドです)。デリゲート メソッド内では、UI に関係するものはすべてメイン キューに送信する必要があります。メソッド内:

- (void) giveKarmaCheck:(BOOL)complete;

UI 呼び出しをラップする

dispatch_async(dispatch_get_main_queue(), ^{
    // Do UI stuff on this thread
});

メイン キューとグローバル キューは同じではないことに注意してください。

于 2013-10-07T04:32:20.183 に答える