0

UITableViewCellsに少し問題があります。tableViewに行数が異なる5つのセクションがあります。UITextfieldを含むサブビューを各行に追加しました。私が抱えている問題は、スクロールすると、セルのサブビューが変化することです。私はビデオを撮ったので、あなたは私が何を意味するかを見ることができます。

http://files.beger.org/Scolling_Issue18072012.swf

すべてドイツ語で申し訳ありません:)

コードは次のとおりです。

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

// Add a UITextFiel
    if ([indexPath section] == 0) {
        UITextField *textFieldName = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        textFieldName.adjustsFontSizeToFitWidth = YES;
        textFieldName.textColor = [UIColor blackColor];
        if ([indexPath row] == 0) {
            textFieldName.placeholder = @"Max";
            textFieldName.keyboardType = UIKeyboardTypeDefault;
            textFieldName.returnKeyType = UIReturnKeyDone;
        }
        else {
            textFieldName.placeholder = @"Mustermann";
            textFieldName.keyboardType = UIKeyboardTypeDefault;
            textFieldName.returnKeyType = UIReturnKeyDone;
        }       
        textFieldName.backgroundColor = [UIColor clearColor];
        textFieldName.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        textFieldName.autocapitalizationType = UITextAutocapitalizationTypeWords; // no auto capitalization support
        textFieldName.textAlignment = UITextAlignmentLeft;
        textFieldName.tag = 0;
        //playerTextField.delegate = self;

        textFieldName.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
        [textFieldName setEnabled: YES];

        [cell addSubview:textFieldName];

        [textFieldName release];
    }
    else if ([indexPath section] == 1) {
        UITextField *textFieldContactData = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        textFieldContactData.adjustsFontSizeToFitWidth = YES;
        textFieldContactData.textColor = [UIColor blackColor];
        if ([indexPath row] == 0) {
            textFieldContactData.placeholder = @"04132 21123 321";
            textFieldContactData.keyboardType = UIKeyboardTypePhonePad;
        }
        else if ([indexPath row] == 1) {
            textFieldContactData.placeholder = @"04132 21123 300";
            textFieldContactData.keyboardType = UIKeyboardTypePhonePad;
        }
        else if ([indexPath row] == 2) {
            textFieldContactData.placeholder = @"0150 12543 101";
            textFieldContactData.keyboardType = UIKeyboardTypePhonePad;
        }
        else if ([indexPath row] == 3) {
            textFieldContactData.placeholder = @"beispiel@musterfirma.de";
            textFieldContactData.keyboardType = UIKeyboardTypeEmailAddress;
            textFieldContactData.returnKeyType = UIReturnKeyDone;
        }
        else if ([indexPath row] == 4) {
            textFieldContactData.placeholder = @"www.musterfirma.de";
            textFieldContactData.keyboardType = UIKeyboardTypeURL;
            textFieldContactData.returnKeyType = UIReturnKeyDone;
        }
        else {
            textFieldContactData.placeholder = @"Deutsch";
            textFieldContactData.keyboardType = UIKeyboardTypeDefault;
            textFieldContactData.returnKeyType = UIReturnKeyDone;
        }
        textFieldContactData.backgroundColor = [UIColor clearColor];
        textFieldContactData.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        textFieldContactData.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
        textFieldContactData.textAlignment = UITextAlignmentLeft;
        textFieldContactData.tag = 0;
        //playerTextField.delegate = self;

        textFieldContactData.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
        [textFieldContactData setEnabled: YES];

        [cell addSubview:textFieldContactData];

        [textFieldContactData release];
    }
    // Adresse
    else if ([indexPath section] == 2) {
        UITextField *textFieldAddress = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        textFieldAddress.adjustsFontSizeToFitWidth = YES;
        textFieldAddress.textColor = [UIColor blackColor];
        if ([indexPath row] == 0) {
            textFieldAddress.placeholder = @"Musterstr. 10";
            textFieldAddress.keyboardType = UIKeyboardTypeDefault;
            textFieldAddress.autocapitalizationType = UITextAutocapitalizationTypeWords;
        }
        else if ([indexPath row] == 1) {
            textFieldAddress.placeholder = @"50000";
            textFieldAddress.keyboardType = UIKeyboardTypeDefault;
        }
        else if ([indexPath row] == 2) {
            textFieldAddress.placeholder = @"Musterstadt";
            textFieldAddress.keyboardType = UIKeyboardTypeDefault;
            textFieldAddress.autocapitalizationType = UITextAutocapitalizationTypeWords;
        }
        else {
            textFieldAddress.placeholder = @"Deutschland";
            textFieldAddress.keyboardType = UIKeyboardTypeDefault;
            textFieldAddress.autocapitalizationType = UITextAutocapitalizationTypeWords;
        }
        textFieldAddress.backgroundColor = [UIColor clearColor];
        textFieldAddress.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        textFieldAddress.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
        textFieldAddress.textAlignment = UITextAlignmentLeft;
        textFieldAddress.tag = 0;
        //playerTextField.delegate = self;

        textFieldAddress.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
        [textFieldAddress setEnabled: YES];

        [cell addSubview:textFieldAddress];

        [textFieldAddress release];
    }
    // Abteilung und Funktion
    else if ([indexPath section] == 3) {
        UITextField *textFieldFunction = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        textFieldFunction.adjustsFontSizeToFitWidth = YES;
        textFieldFunction.textColor = [UIColor blackColor];
        if ([indexPath row] == 0) {
            textFieldFunction.placeholder = @"Einkauf";
            textFieldFunction.keyboardType = UIKeyboardTypeDefault;
            textFieldFunction.autocapitalizationType = UITextAutocapitalizationTypeWords;
        }
        else {
            textFieldFunction.placeholder = @"Einkaufsleiter";
            textFieldFunction.keyboardType = UIKeyboardTypeDefault;
            textFieldFunction.autocapitalizationType = UITextAutocapitalizationTypeWords;
        }
        textFieldFunction.backgroundColor = [UIColor clearColor];
        textFieldFunction.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        textFieldFunction.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
        textFieldFunction.textAlignment = UITextAlignmentLeft;
        textFieldFunction.tag = 0;
        //playerTextField.delegate = self;

        textFieldFunction.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
        [textFieldFunction setEnabled: YES];

        [cell addSubview:textFieldFunction];

        [textFieldFunction release];
    }
    // Kundenbeziehung
    else if ([indexPath section] == 4) {
        UITextField *textFieldCustomer= [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        textFieldCustomer.adjustsFontSizeToFitWidth = YES;
        textFieldCustomer.textColor = [UIColor blackColor];
        textFieldCustomer.placeholder = @"Musterfirma AG";
        textFieldCustomer.keyboardType = UIKeyboardTypeDefault;
        textFieldCustomer.backgroundColor = [UIColor clearColor];
        textFieldCustomer.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        textFieldCustomer.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
        textFieldCustomer.textAlignment = UITextAlignmentLeft;
        textFieldCustomer.tag = 0;

        textFieldCustomer.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
        [textFieldCustomer setEnabled: YES];

        [cell addSubview:textFieldCustomer];

        [textFieldCustomer release];
    }
    // Lieferantenbeziehung
    else {
        UITextField *textFieldSupplier= [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        textFieldSupplier.adjustsFontSizeToFitWidth = YES;
        textFieldSupplier.textColor = [UIColor blackColor];
        textFieldSupplier.placeholder = @"Lieferanten AG";
        textFieldSupplier.keyboardType = UIKeyboardTypeDefault;
        textFieldSupplier.backgroundColor = [UIColor clearColor];
        textFieldSupplier.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        textFieldSupplier.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
        textFieldSupplier.textAlignment = UITextAlignmentLeft;
        textFieldSupplier.tag = 0;

        textFieldSupplier.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
        [textFieldSupplier setEnabled: YES];

        [cell addSubview:textFieldSupplier];

        [textFieldSupplier release];
    }
}
if ([indexPath section] == 0) {
    if ([indexPath row] == 0) { 
        // Vorname
        cell.textLabel.text = @"Vorname";
    }
    else {
        // Nachname
        cell.textLabel.text = @"Nachname";
    }
}
// Kontaktdaten
else if ([indexPath section] == 1) {
    if ([indexPath row] == 0) { 
        cell.textLabel.text = @"Telefon";
    }
    else if ([indexPath row] == 1) {
         cell.textLabel.text = @"Fax";
    }
    else if ([indexPath row] == 2) {
        cell.textLabel.text = @"Mobil";
    }
    else if ([indexPath row] == 3) {
        cell.textLabel.text = @"E-Mail";
    }
    else if ([indexPath row] == 4) {
        cell.textLabel.text = @"WWW";
    }
    else {
        cell.textLabel.text = @"Sprache";
    }
}
// Adresse
else if ([indexPath section] == 2) {
    if ([indexPath row] == 0) { 
        cell.textLabel.text = @"Straße";
    }
    else if ([indexPath row] == 1) {
        cell.textLabel.text = @"PLZ";
    }
    else if ([indexPath row] == 2) {
        cell.textLabel.text = @"Ort";
    }
    else {
        cell.textLabel.text = @"Land";
    }
}
// Section Abteilung und Funktion
else if ([indexPath section] == 3) {
    if ([indexPath row] == 0) { 
        cell.textLabel.text = @"Abteilung";
    }
    else {
        cell.textLabel.text = @"Funktion";
    }
}
// Section Kundenbeziehung
else if ([indexPath section] == 4) {
        cell.textLabel.text = @"Kunde";
}
// Section Lieferantenbeziehung
else {
    cell.textLabel.text = @"Lieferant";
}
return cell;    

}

再利用すると、セルにデータ/サブビューを保存できないと思います。問題を解決する方法はありますか?

ありがとう!クリストフ

4

2 に答える 2

2

dequeueReusableCellWithIdentifierは、再利用可能なセルがある場合はそれを返します。これは、一部のセルが表示されていない場合、iOSがUITableViewCellを再度作成しないことを意味します。使用済みセルを直接返します。パフォーマンスが向上し、メモリ使用率が低くなります。

したがって、セルがnilであると識別した場合は、セルとテキストフィールドを作成する必要があります。textfiledをセルのサブビューとして追加します。

セルの外側がnilの場合、textfiledの他の属性を指定する必要があります。cell.textLable.textに対して同じことを行いました。textfiledについても同じ演習を行う必要があります。textFieldをフェッチするには、追加中にタグを付けます。そのタグを使用して、textFieldをフェッチして戻すことができます。

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

// Add a UITextFiel
    if ([indexPath section] == 0) {
        UITextField *textFieldName = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        textFieldName.adjustsFontSizeToFitWidth = YES;
        textFieldName.textColor = [UIColor blackColor];
        textFieldName.tag = 25;
        if ([indexPath row] == 0) {
            textFieldName.placeholder = @"Max";
            textFieldName.keyboardType = UIKeyboardTypeDefault;
            textFieldName.returnKeyType = UIReturnKeyDone;
 textFieldName.backgroundColor = [UIColor clearColor];
        textFieldName.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        textFieldName.autocapitalizationType = UITextAutocapitalizationTypeWords; // no auto capitalization support
        textFieldName.textAlignment = UITextAlignmentLeft;
[cell addSubview:textFieldName];

        [textFieldName release];
        }
    }
}

//Here fetch the textFiled..
UITextFiled *tField = [cell viewWithTag:25];
//Identify row, section and do other operations like setting text, place holder etc...
于 2012-07-18T09:22:59.547 に答える
0

セルが新しく作成されたセルである場合にのみUITextViewを追加するため、セルが再利用されると、UITextviewも再利用されます。これを回避するには、次のいずれかを実行できます。

すべてのUITextView作成コードをif(cell == nil)ステートメントから移動します

また

cell.textlabel.textを設定するときは、セルにすでに追加されているテキストビューにアクセスする方法を見つけ(タグプロパティを使用するか、サブビューをループします)、変更を加えます。

お役に立てれば。

于 2012-07-18T09:19:23.377 に答える