2

ここで奇妙な問題が発生しています。TableView があり、indexPath.row を UIButton のタグに割り当てて、そのタグをセグエのパラメーターとして渡し、オブジェクトを次のビュー コントローラーに渡すことができます。ボタンを押すと配列から間違ったオブジェクトが渡されたときに、TableViewで6つのセルを超えるまで、すべてがうまく機能します。何が起こっているのかを確認するためにいくつかの NSLog を入れることにしましたが、結果は非常に奇妙であることがわかりました。割り当てbuttonForApplyingTag.tag = indexPath.row;は NSLogs の直後に発生し、 の前の最後のステートメントreturn cell;です。indexPath.row の実際の値が正しいため、配列からのオブジェクトが適切にロードされています。問題を引き起こしているのはボタンだけです。ここに私の出力があります:

2012-11-28 19:01:57.596  IndexPath.row: 0
2012-11-28 19:01:57.596  IndexPath.Row from Tag: 0
2012-11-28 19:01:57.597  SearchResults Count: 100
2012-11-28 19:01:57.598  IndexPath.row: 1
2012-11-28 19:01:57.598  IndexPath.Row from Tag: 1
2012-11-28 19:01:57.598  SearchResults Count: 100
2012-11-28 19:01:57.599  IndexPath.row: 2
2012-11-28 19:01:57.600  IndexPath.Row from Tag: 2
2012-11-28 19:01:57.600  SearchResults Count: 100
2012-11-28 19:01:57.601  IndexPath.row: 3
2012-11-28 19:01:57.601  IndexPath.Row from Tag: 3
2012-11-28 19:01:57.602  SearchResults Count: 100
2012-11-28 19:01:57.602  IndexPath.row: 4
2012-11-28 19:01:57.603  IndexPath.Row from Tag: 4
2012-11-28 19:01:57.603  SearchResults Count: 100
2012-11-28 19:01:57.604  IndexPath.row: 5
2012-11-28 19:01:57.605  IndexPath.Row from Tag: 5
2012-11-28 19:01:57.605  SearchResults Count: 100
2012-11-28 19:02:02.004  IndexPath.row: 6
2012-11-28 19:02:02.004  IndexPath.Row from Tag: 6
2012-11-28 19:02:02.005  SearchResults Count: 100
2012-11-28 19:02:03.993  IndexPath.row: 7
2012-11-28 19:02:03.993  IndexPath.Row from Tag: 0
2012-11-28 19:02:03.993  SearchResults Count: 100
2012-11-28 19:02:17.846  IndexPath.row: 8
2012-11-28 19:02:17.846  IndexPath.Row from Tag: 0
2012-11-28 19:02:17.846  SearchResults Count: 100
2012-11-28 19:02:18.482  IndexPath.row: 9
2012-11-28 19:02:18.482  IndexPath.Row from Tag: 0
2012-11-28 19:02:18.482  SearchResults Count: 100

行数が多すぎて申し訳ありませんが、アイデアはわかります。この傾向は 100 まで続き、ボタンが正しいタグを受け取っていないことを意味します。さらに奇妙なのは、偽の indexPath.row タグを含むセルでボタンをクリックすると、明らかに別のタグが作成され、値の低いタグが取得されるという事実です。さらに、セル 6 が TableView の下部に部分的に表示されている場合、セルが実際に読み込まれたことは明らかですが、NSLogs がデバッガーにポストされていないため、ボタンは失敗し、5 のタグが返されます。この方法には、実際には 3 つの問題があります。

本当に聞きたいのは、なぜこの奇妙な動作が起こっているのか、どうすれば修正できるのかということです。第二に、これは単なる災害のように見えるため、ボタンがクリックされたときに配列からオブジェクトを取得できるように、indexPath.row 値を渡すより良い方法はありますか?

助けてくれてありがとう。
よろしく、
マイク

PS 全くの素人なので、リーマン的に説明していただけるとありがたいです。

編集: 要求に応じて、実際にタグをボタンに割り当てるコードを次に示します。

    UIButton *buttonForApplyingTag = (UIButton *)[cell viewWithTag:1004];

    NSLog(@"IndexPath.row: %d", indexPath.row);
    NSLog(@"IndexPath.Row from Tag: %d", buttonForApplyingTag.tag);
    NSLog(@"SearchResults Count: %d", [searchResults count]);
    buttonForApplyingTag.tag = indexPath.row;

    return cell;

編集 2: cellForRowAtIndexPath コード全体を次に示します。

if (isLoading) {
    return [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
} else if ([searchResults count] == 0) {
    return [tableView dequeueReusableCellWithIdentifier:NothingFoundCellIdentifier];
} else {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchResultCellIdentifier];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [tableView setSeparatorColor:[UIColor grayColor]];
    /* Then we do a load of assignments for labels in the cell, then we reach the tagging code. */

SearchResultCellIdentifierこれから来ます:static NSString *const SearchResultCellIdentifier = @"SearchResultCellProto";

4

2 に答える 2

5

やりたいことは、セルにボタンを配置し、ボタン アクションが発生したときにどのセルのボタンが押されたかを知ることです。タグを使用することは合理的なアイデアですが、セルが再利用される (そして別のインデックス パスになる) ときにタグを変更する必要があるため、実装するのは困難です。

より良いアプローチは次のとおりです。定数タグを使用して(またはタグなしで)ボタンを追加し、クリックアクションでこれを行います...

- (IBAction)pressedButtonInMyCell:(id)sender {

    UIButton *button = (UIButton *)sender;

    // find the cell that contains the button, this might be one or two levels
    // up depending on how you created the button (one level in code, two in IB, probably)
    UIView *view = button.superview;
    while (view && ![view isKindOfClass:[UITableViewCell self]]) view = view.superview;

    UITableViewCell *cell = (UITableViewCell *)view;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSLog(@"cell is in section %d, row %d", indexPath.section, indexPath.row);
}
于 2012-11-28T20:07:03.183 に答える