2

「dequeueReusableCellWithIdentifier」の問題についてスタックで多くのことを読みましたが、いくつかの回答を試しましたが、修正できないようです。誰かが私のコードの問題を見つけてくれれば幸いです

部分コード:

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView   
{
return 10;
}



- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)

{

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

if (indexPath.section == 0)   {

    if (indexPath.row == 0) {

        cell.textLabel.text = [arrayOfQuestion objectAtIndex:indexPath.section];

    }

    else if (indexPath.row == 1) {

        question1 = [[UITextField alloc] initWithFrame: CGRectMake(20, 3, 280, 38) ];

        question1.adjustsFontSizeToFitWidth = YES;

        question1.textColor = [UIColor blackColor];

        question1.font = [UIFont systemFontOfSize:17.0];

        question1.backgroundColor = [UIColor blueColor];

        question1.autocorrectionType = UITextAutocorrectionTypeNo;        // no auto correction support

        question1.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support

        question1.textAlignment = UITextAlignmentRight;

        question1.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)

        question1.returnKeyType = UIReturnKeyDone;

        question1.tag = 0;

//            question1.delegate = self;



        question1.clearButtonMode = UITextFieldViewModeUnlessEditing; // no clear 'x' button to the right



        [question1 setEnabled: YES ];

        [cell addSubview: question1 ];

    }

}

else if (indexPath.section == 1) {

    if (indexPath.row == 0) {

        cell.textLabel.text = [arrayOfQuestion objectAtIndex:indexPath.section];

    }

    else if (indexPath.row == 1) {

        question2 = [ [ UITextField alloc ] initWithFrame: CGRectMake(20, 3, 280, 38) ];

        question2.adjustsFontSizeToFitWidth = YES;

        question2.textColor = [UIColor blackColor];

        question2.font = [UIFont systemFontOfSize:17.0];

        question2.backgroundColor = [UIColor blueColor];

        question2.autocorrectionType = UITextAutocorrectionTypeNo;        // no auto correction support

        question2.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support

        question2.textAlignment = UITextAlignmentRight;

        question2.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)

        question2.returnKeyType = UIReturnKeyDone;

        question2.tag = 1;

        //            listTitleTextField.delegate = self;



        question2.clearButtonMode = UITextFieldViewModeUnlessEditing; // no clear 'x' button to the right



        [question2 setEnabled: YES ];

        [cell addSubview: question2 ];

    }

}

................

return cell;

}
4

1 に答える 1

1

dequeueReusableCellWithIdentifier同じオブジェクトを何度も再利用してセルを描画することを忘れないでください。テーブル ビューで不明な行数に対して新しいセルを作成するよりも効率的です。これは[cell addSubview: ... ];、次にそのセルが必要になったときに UITextFields を追加すると、そのサブビューがビューに追加されていることを意味します。

既に追加され、プロパティとしてアクセス可能なUITableViewCellのサブクラスを作成することをお勧めします。UITextField次に、2 つのセル識別子を使用できます。1 つは基本的な質問を参照し、UITableViewCellもう 1 つは新しいサブクラスである回答を参照します。

その上で、テーブル セルの作成方法をリファクタリングして、コードをより柔軟にすることを検討します。そのままではあまりスケーラブルではなく、答えにアクセスしようとするのは悪夢に違いありません。

于 2013-04-09T21:02:01.903 に答える