【问题标题】:Scrolling Issue with UITableViewCell and UITextFieldUITableViewCell 和 UITextField 的滚动问题
【发布时间】:2012-07-18 08:03:09
【问题描述】:

我的 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;    

}

我认为当我重用单元格时,它们无法存储我的数据/子视图。 有什么办法可以解决这个问题吗?

谢谢! 克里斯托夫

【问题讨论】:

  • 你能发布一些代码吗?特别是 cellForRowAtIndexPath 方法
  • 是的!我不知道为什么我还没有这样做:)

标签: objective-c ios uitableview uitextfield


【解决方案1】:

dequeueReusableCellWithIdentifier 返回可重用单元格(如果有)。这意味着如果某些单元格不可见,iOS 将不会再次创建 UITableViewCell。它将直接返回使用的单元格。它使性能更好,内存利用率更低。

因此,当您确定单元格为 nil 时,您应该创建一个单元格和文本字段。将文本文件添加为单元格的子视图。

单元格外为nil,你应该提供textfiled的其他属性。你对 cell.textLable.text 做了同样的事情。您还需要为 textfiled 进行相同的练习。要获取文本字段,请在添加时添加标签。使用该标签,您可以取回 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...

【讨论】:

  • 感谢您的回答!我会马上试试这个!有道理:)
【解决方案2】:

只有在单元格是新创建的单元格时才添加 UITextView,因此如果重新使用该单元格,则 UITextview 也会被重新使用。要解决这个问题,您可以:

将所有 UITextView 创建代码移出 if (cell == nil) 语句

当您设置 cell.textlabel.text 时,找到一种访问已添加到单元格中的 textview 的方法(使用 tag 属性,或循环遍历子视图)并对其进行更改。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多