0

我有一个带有常规 UITableViewCell 的 UITableView,但我不使用任何 UITableViewCell 的标签。我只是使用单元格嵌入标签和 UITextField 来输入一些数据。问题是当您向上或向下滚动并且 UITableviewCell 重新绘制自身时,它会在旧的 UITextFieldView 上绘制重叠的 UITextFieldView 并且您会看到双打!我在想,也许因为我确实将这些 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

您忘记了这样的条件:

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 回答
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 回答