1

私はこれに何時間も本当に困惑していたので、助けを求める時が来たと思いました!

サブクラス ( )UITextFieldに埋め込まれているのテキスト値にアクセスしようとしています。UITableViewCellQuestionCell

問題は、メソッドでセルにアクセスしようとすると、cellForRowAtIndexPath(NULL) が返されることです。

QuestionCell *qc = (QuestionCell*)[questionsTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSLog(@"qc %@",qc); // Gives "qc (NULL)" if there's text in the custom cell

UITextFieldこれは、カスタム セルにテキストが入力されている場合にのみ発生します。テキストがない場合、期待どおりにセルが返されます。

私はストーリーボードでXCode4.2を使用しており、カスタムスタイルとクラスを使用していますUITableViewCell

ここに私のQuestionCell.hがあります

#import <UIKit/UIKit.h>

@interface QuestionCell : UITableViewCell


@property (weak, nonatomic) IBOutlet UILabel *correctionLabel;
@property (weak, nonatomic) IBOutlet UIImageView *correctionMark;
@property (weak, nonatomic) IBOutlet UILabel *pronounLabel;
@property (weak, nonatomic) IBOutlet UITextField *answerInput;

@end

そしてセルは通常のデリゲートメソッドで作成されます

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0)
    {
        QuestionCell *cell = nil;
        cell = [tableView dequeueReusableCellWithIdentifier:@"QuestionCell"];

        NSString *pronoun = nil;

        switch (indexPath.row) {
            case 0:
                pronoun = @"je";
                break;
            case 1:
                pronoun = @"tu";
                break;
            case 2:
                pronoun = @"il";
                break;
            case 3:
                pronoun = @"nous";
                break;
            case 4:
                pronoun = @"vous";
                break;
            case 5:
                pronoun = @"ils";
                break;
            default:
                break;
        }

        cell.pronounLabel.text = pronoun;

        return cell;
    }
    if (indexPath.section == 1)
    {
        ActionCell *cell = nil;
        cell = [tableView dequeueReusableCellWithIdentifier:@"ActionCell"];
        return cell;
    }
    return nil;
}
4

1 に答える 1

1

上記のコードに問題はありません。QuestionCell私はテストプロジェクトを作成し、これ(UITextFieldおよびUILabelオブジェクトを含む)とあなたを挿入しましたtableView:cellForRowAtIndexPath。すべてが正常です。

このUITableViewメソッドは、セルが画面からスクロールアウトした場合にcellForRowAtIndexPathのみ返されます(これは予想される動作です)。nilしかし、これらの6つのセルが存在する状態で、テーブルの `cellForRowAtIndexPathを繰り返し処理し、テキストを入力したかどうかに関係なく、すべてが正常でした。

その後のコメントから判断すると、セルが画面からスクロールアウトしたように見えます。うん、それは間違いなくcellForRowAtIndexPath戻ってくるだろうnil

于 2012-10-02T11:58:22.640 に答える