1

で を作成しUITextFieldましたUITableView。データを入力してキーボードを閉じます。ただし、下にスクロールして非表示にしてUITextFieldから再度上にスクロールすると、以下に示すように「UITextField」データが複製されます。

ビューの元のロード:

入力データ:ここに画像の説明を入力

非表示のテキストフィールドの後、再度編集を開始します。ここに画像の説明を入力

- (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) { // Email & Password Section
    cell.textLabel.text = @"Subject";
} else {
    cell.textLabel.text = @"Task";
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

if ([indexPath section] == 0) {
    UITextField *subject = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
    subject.adjustsFontSizeToFitWidth = YES;
    subject.textColor = [UIColor blackColor];
    if ([indexPath row] == 0) {
        subject.placeholder = @"Maths";
        subject.keyboardType = UIKeyboardTypeEmailAddress;
        subject.returnKeyType = UIReturnKeyNext;
    }
    subject.backgroundColor = [UIColor clearColor];
    subject.autocorrectionType = UITextAutocorrectionTypeNo;
    subject.autocapitalizationType = UITextAutocapitalizationTypeWords;
    subject.tag = 0;
    subject.clearButtonMode = UITextFieldViewModeNever;
    [cell.contentView addSubview:subject];
} else {
    UITextView *task = [[UITextView alloc] initWithFrame:CGRectMake(102, 0, 185, 40)];
    task.text = @"fasfashfjasfhasfasdjhasgdgasdhjagshjdgashjdgahjsdghjasgasdashgdgjasd";
    task.editable = NO;
    task.scrollEnabled = NO;
    task.userInteractionEnabled = NO;
    task.textColor = [UIColor colorWithRed: 62.0/255.0 green: 85.0/255.0 blue:132.0/255.0 alpha:1.0];
    task.backgroundColor = [UIColor clearColor];
}



return cell;

}

4

2 に答える 2

4

リチャードが言ったように、セルは再利用されます(それが識別子の目的です)。そのため、によって返される値をテストtableView:cellForRowAtIndexPath:nilますdequeueReusableCellWithIdentifier:。セルがすでに存在し(つまり、以前に割り当てられていて)表示されなくなった場合dequeueReusableCellWithIdentifier:は、このセルを使用して、新しく表示されたセルの内容を表示します。

あなたがしていることは、あなたのセルが表示され、作成されないUITextViewたびにあなたを追加することです。したがって、セルが画面からスクロールアウトされて新しいセルが表示されるたびに、セルに新しいセルが追加されます。メソッドの一部にのみサブビューを追加する必要があります。セルの内容はかなり異なるため、2つの異なる識別子を使用することをお勧めします。UITextViewif (cell == nil)

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

    static NSString *CellIdentifierForSection0 = @"Cell0";
    static NSString *CellIdentifierForSection1 = @"Cell1";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: [indexPath section] == 0 ? CellIdentifierForSection0 : CellIdentifierForSection1];
    if (cell == nil) {

        if ([indexPath section] == 0) { // Email & Password Section

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifierForSection0];
            cell.textLabel.text = @"Subject";

            UITextField *subject = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
            subject.adjustsFontSizeToFitWidth = YES;
            subject.textColor = [UIColor blackColor];
            if ([indexPath row] == 0) {
                subject.placeholder = @"Maths";
                subject.keyboardType = UIKeyboardTypeEmailAddress;
                subject.returnKeyType = UIReturnKeyNext;
            }
            subject.backgroundColor = [UIColor clearColor];
            subject.autocorrectionType = UITextAutocorrectionTypeNo;
            subject.autocapitalizationType = UITextAutocapitalizationTypeWords;
            subject.tag = 0;
            subject.clearButtonMode = UITextFieldViewModeNever;
            [cell.contentView addSubview:subject];
        } else {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifierForSection1];
            cell.textLabel.text = @"Task";

            UITextView *task = [[UITextView alloc] initWithFrame:CGRectMake(102, 0, 185, 40)];
            task.text = @"fasfashfjasfhasfasdjhasgdgasdhjagshjdgashjdgahjsdghjasgasdashgdgjasd";
            task.editable = NO;
            task.scrollEnabled = NO;
            task.userInteractionEnabled = NO;
            task.textColor = [UIColor colorWithRed: 62.0/255.0 green: 85.0/255.0 blue:132.0/255.0 alpha:1.0];
            task.backgroundColor = [UIColor clearColor];
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    return cell;
}

このコードは主に例の目的であり、大幅に削減される可能性があることに注意してください。さらに、Richardが提案したようなサブクラスを使用する必要がありますUITableViewCell。これは、コードを整理して再利用しやすくするのに役立ちます。

ただし、サブビューdrawRect:の追加には使用しないでください。これは不要であり、パフォーマンスに影響を与えます。CoreAnimationやCoreGraphicsのように実際のdrawRect:描画を作成する場合にのみ使用してください。サブビューの追加は、Interface Builderの使用中、または使用状況に応じて行う必要があります。initWithFrame:initWithCoder:

于 2013-03-16T14:11:54.270 に答える
0

セルは再利用されるため、再利用されるたびにサブビューが追加されることに注意してください。セルにサブビューを追加する場合は、サブクラスを作成し、そのサブクラスUITableViewCellのメソッドにサブビューを追加することをお勧めしdrawRect:ます。このように、変更はセルの一部であり、セルが再利用されるたびに追加されるわけではありません。

于 2013-03-16T14:00:17.813 に答える