0

通常のUITableViewCellを備えたUITableViewがありますが、UITableViewCellのラベルは使用していません。セルを使用してラベルを埋め込み、UITextFieldを使用してデータを入力します。問題は、上または下にスクロールしてUITableviewCellがそれ自体を再描画すると、古いものの上に重複するUITextFieldViewが描画され、doubleが表示されることです。これらのUITextFieldsを辞書に入れているので、強力なポインターでテキストフィールドを保存し、別のUITextFieldsを作成してオーバーラップしようとする可能性があると思います。誰か提案がありますか?

これが私のコードです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ProductCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSDictionary *product = [self.products objectAtIndex:indexPath.row];
NSDictionary *orderpoint = [self.orderpoints objectAtIndex:indexPath.row];

cell.textLabel.text = @""; //black out text

CGFloat calculatedHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UILabel *productLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.bounds.size.width - 50.0, calculatedHeight)];

productLabel.text = [[NSString alloc] initWithFormat:@"%@. %@",
                       [orderpoint objectForKey:@"sequence_nr"], [product objectForKey:@"name"]];
//word wrapping
productLabel.lineBreakMode = UILineBreakModeWordWrap;
productLabel.numberOfLines = 0; //infinite number of lines
productLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];

[cell.contentView addSubview:productLabel];

 //create the cell's textfield
UITextField *cellTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.bounds.size.width - 50, cell.bounds.size.height - 30, 50, calculatedHeight - 20)];
cellTextField.adjustsFontSizeToFitWidth = YES;
cellTextField.textColor = [UIColor blackColor];
cellTextField.keyboardType = UIKeyboardTypeNumberPad; // will only need to end a count
cellTextField.returnKeyType = UIReturnKeyDone; // TODO make it go to the next items key, or make it exit out
cellTextField.backgroundColor = [UIColor whiteColor];
cellTextField.textAlignment = UITextAlignmentLeft; //align to the right
//cellTextField.delegate = self; //will need to set delegate, maybe
cellTextField.clearButtonMode = UITextFieldViewModeNever;
cellTextField.enabled = YES;
cellTextField.borderStyle = UITextBorderStyleRoundedRect; //add bezel rounded look to textfield
cellTextField.delegate = self;

[cell.contentView addSubview: cellTextField]; //add the textfield to the cell
// save to dictionary, using a dictionary because not certain if this is created in order to use an Array
[self.textFieldDict setObject:cellTextField forKey:[[NSNumber alloc] initWithInteger:indexPath.row]];

return cell;
}
4

4 に答える 4

0

セルを初期化していません。これを試してください:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"ProductCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//It will check whether cell in there or not, then deque the cell...
    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

    NSDictionary *product = [self.products objectAtIndex:indexPath.row];
    NSDictionary *orderpoint = [self.orderpoints objectAtIndex:indexPath.row];

    cell.textLabel.text = @""; //black out text

    CGFloat calculatedHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    UILabel *productLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.bounds.size.width - 50.0, calculatedHeight)];

    productLabel.text = [[NSString alloc] initWithFormat:@"%@. %@",
                           [orderpoint objectForKey:@"sequence_nr"], [product objectForKey:@"name"]];
    //word wrapping
    productLabel.lineBreakMode = UILineBreakModeWordWrap;
    productLabel.numberOfLines = 0; //infinite number of lines
    productLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];

    [cell.contentView addSubview:productLabel];

     //create the cell's textfield
    UITextField *cellTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.bounds.size.width - 50, cell.bounds.size.height - 30, 50, calculatedHeight - 20)];
    cellTextField.adjustsFontSizeToFitWidth = YES;
    cellTextField.textColor = [UIColor blackColor];
    cellTextField.keyboardType = UIKeyboardTypeNumberPad; // will only need to end a count
    cellTextField.returnKeyType = UIReturnKeyDone; // TODO make it go to the next items key, or make it exit out
    cellTextField.backgroundColor = [UIColor whiteColor];
    cellTextField.textAlignment = UITextAlignmentLeft; //align to the right
    //cellTextField.delegate = self; //will need to set delegate, maybe
    cellTextField.clearButtonMode = UITextFieldViewModeNever;
    cellTextField.enabled = YES;
    cellTextField.borderStyle = UITextBorderStyleRoundedRect; //add bezel rounded look to textfield
    cellTextField.delegate = self;

    [cell.contentView addSubview: cellTextField]; //add the textfield to the cell

    return cell;
    }
于 2012-06-19T06:23:19.990 に答える
0

再利用性を使用せず、常にセルを割り当てるか、

デキュー後にチェックを行う(このように)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell==nil)
{
  // make text field and label an add tag
}

//and outside this by using tag fetch the labels and textField and clear the textFields.
于 2012-06-19T06:24:50.673 に答える
0

あなたはこのような条件を置くのを忘れました:

if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
         }
于 2012-06-19T06:25:21.720 に答える
0

これを参照として使用しました:UITableViewCellにサブビューを追加します

このビューが以前から追加されたかどうかを最初に確認しました。追加された場合は、再度追加しないでください。セルがゼロであることとは何の関係もありません。私が何かを逃したのでなければ?私が知っているのは、これが今はうまく機能しているようだということだけです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ProductCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSDictionary *product = [self.products objectAtIndex:indexPath.row];
NSDictionary *orderpoint = [self.orderpoints objectAtIndex:indexPath.row];

cell.textLabel.text = @""; //black out text

CGFloat calculatedHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UILabel *productLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.bounds.size.width - 50.0, calculatedHeight)];

productLabel.text = [[NSString alloc] initWithFormat:@"%@. %@",
                       [orderpoint objectForKey:@"sequence_nr"], [product objectForKey:@"name"]];
//word wrapping
productLabel.lineBreakMode = UILineBreakModeWordWrap;
productLabel.numberOfLines = 0; //infinite number of lines
productLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];

[cell.contentView addSubview:productLabel];

if (![cell viewWithTag:1])
{
    //create the cell's textfield
    UITextField *cellTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.bounds.size.width - 50, cell.bounds.size.height - 30, 50, calculatedHeight - 20)];
    cellTextField.adjustsFontSizeToFitWidth = YES;
    cellTextField.textColor = [UIColor blackColor];
    cellTextField.keyboardType = UIKeyboardTypeNumberPad; // will only need to end a count
    cellTextField.returnKeyType = UIReturnKeyDone; // TODO make it go to the next items key, or make it exit out
    cellTextField.backgroundColor = [UIColor whiteColor];
    cellTextField.textAlignment = UITextAlignmentLeft; //align to the right
    cellTextField.delegate = self; //will need to set delegate, maybe
    cellTextField.clearButtonMode = UITextFieldViewModeNever;
    cellTextField.enabled = YES;
    cellTextField.borderStyle = UITextBorderStyleRoundedRect; //add bezel rounded look to textfield
    cellTextField.delegate = self;

    cellTextField.tag = 1; //set tag to 1

    [cell.contentView addSubview: cellTextField]; //add the textfield to the cell
    // save to dictionary, using a dictionary because not certain if this is created in order to use an Array
    [self.textFieldDict setObject:cellTextField forKey:[[NSNumber alloc] initWithInteger:indexPath.row]];
}

return cell;

}

于 2012-06-19T06:39:50.557 に答える