0

登録ページ用に、UITextField ごとに少なくとも 10 個のセルを作成しました。最初のセルのテキストフィールドに単語を挿入し、tableView をスクロールすると、別のセルにあるテキストフィールドに入力した単語が表示されます。

以下は私のコードです。入力した textField データがループしています。これは dequeueReusableCellWithIdentifier が原因です...どうすればこの問題を解決できますか? どうもありがとうございました。

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


static NSString *CellIdentifier = @"RCell";
RegisterCell *cell = (RegisterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
if (cell == nil)
    cell = [[[RegisterCell alloc] initWithFrame:CGRectMake(0, 0, 280, 44) reuseIdentifier:CellIdentifier] autorelease];


if(indexPath.section == 0){
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]];
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
    cell.registerLabel.text = mainLabel;

    UITextField *valTxtField = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 180, 30)];
    valTxtField.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:18.0];
    valTxtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    valTxtField.delegate = self;
    valTxtField.returnKeyType = UIReturnKeyDone;
    valTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
    valTxtField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    if(indexPath.row == 0)
    {
        valTxtField.text = @"";
        emailTxtFld = valTxtField; //emailTxtFld is global variable
    }
    if(indexPath.row == 1)
    {
        valTxtField.text = @"";
        reEmailTxtFld = valTxtField; //reEmailTxtFld is global variable
    }

    [cell.contentView addSubview:valTxtField];
    [valTxtField release];
}    
else if(indexPath.section == 1){
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+10]; 
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
    cell.registerLabel.text = mainLabel;
    cell.registerTextField.enabled = NO;
}
else if(indexPath.section == 2){
    if(indexPath.row == 0){
        NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+11];
        NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
        cell.registerLabel.text = mainLabel;
        cell.registerTextField.enabled = NO;
    }
}


return cell;

}

4

2 に答える 2

1

簡単な方法は、サブビューを追加する前に、セル contentView からすべてのサブビューを削除することです。例:

for (UIView *subview in [cell.contentView subviews])
    [subview removeFromSuperview];

より効率的な方法は、if (cell == nil) ステートメント内ですべてのセルの作成を行うことですが、それはテーブル内のセルの数によって異なります。

于 2012-05-24T09:44:46.913 に答える
0

適切な実装は、if (cell == nil) 内の任意のビューの作成を移動することです。

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


static NSString *CellIdentifier = @"RCell";
RegisterCell *cell = (RegisterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];    

UITextField *valTxtField;
if (cell == nil)
{
    cell = [[[RegisterCell alloc] initWithFrame:CGRectMake(0, 0, 280, 44) reuseIdentifier:CellIdentifier] autorelease];

if(indexPath.section == 0){
    valTxtField = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 180, 30)];
    valTxtField.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:18.0];
    valTxtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    valTxtField.delegate = self;
    valTxtField.returnKeyType = UIReturnKeyDone;
    valTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
    valTxtField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    valTxtField.tag = 100;

    [cell.contentView addSubview:valTxtField];
    [valTxtField release];
}
}

valTxtField = (UITextField *)[cell.contentView viewWithTag:100];


if(indexPath.section == 0){
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]];
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
    cell.registerLabel.text = mainLabel;

    if(indexPath.row == 0)
    {
        valTxtField.text = @"";
        emailTxtFld = valTxtField; //emailTxtFld is global variable
    }
    if(indexPath.row == 1)
    {
        valTxtField.text = @"";
        reEmailTxtFld = valTxtField; //reEmailTxtFld is global variable
    }
}    
else if(indexPath.section == 1){
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+10]; 
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
    cell.registerLabel.text = mainLabel;
    cell.registerTextField.enabled = NO;
}
else if(indexPath.section == 2){
    if(indexPath.row == 0){
        NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+11];
        NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]];
        cell.registerLabel.text = mainLabel;
        cell.registerTextField.enabled = NO;
    }
}


return cell;
于 2012-05-24T09:45:40.717 に答える