【问题标题】:Two Textfield in uitableview issueuitableview 问题中的两个文本字段
【发布时间】:2012-07-31 05:00:34
【问题描述】:

我有一个 2 行 n 4 个部分的 uitableview,并且我在每个单元格中有一个 标签和文本字段。问题是我的标签出现在所有部分中,因为我的文本字段在其他部分中没有重复。实际上,每个单元格都有两个文本字段,一个是普通文本字段,另一个是选择器文本字段(当我单击 TF 时,选择器会弹出)。 对于一个部分,TF 都来了,但在其他部分没有重复

我的代码

static NSString *CellIdentifier = @"Cell";

UITextField *textField;

NSString *string=[NSString stringWithFormat:@"ident_%d",indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       }
cell.selectionStyle=UITableViewCellSelectionStyleNone;

UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 35)];
[lbl1 setFont:[UIFont systemFontOfSize:16]];
[lbl1 setTextColor:[UIColor blackColor]];
[lbl1 setBackgroundColor:[UIColor clearColor]];

if (indexPath.row==0) {

    lbl1.text = @"Quantity";
    [cell.contentView addSubview:self.qntTF];

}

if (indexPath.row==1) {
    lbl1.text = @"Unit";
    [cell.contentView addSubview:self.unitTF];
  }

// Configure the cell...;
textField  =[self.tableArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:textField];
cell.textLabel.text = nil;
textField.tag = TextFieldTag;
cell.detailTextLabel.text = nil;
[cell addSubview:lbl1];
[lbl1 release];

return cell;

【问题讨论】:

  • 嗨 - 请再看看我的回答。那里的模式将解决您的问题。您添加了太多标签(每次绘制单元格时添加一个标签),并且您还一遍又一遍地添加相同的文本字段。因为它是同一个,它只是从一个单元格移动到另一个单元格。您只会在最后调用渲染的单元格上看到它。
  • @danh 我试过了,现在的问题是它没有被所有单元格调用,它只被两个部分调用,1 个保留为空,带有标签而不是 texfield。我的选择器现在出现问题

标签: iphone ios5 uitableview uitextfield


【解决方案1】:

现在的代码方式是,您添加的标签比您想象的要多得多(每次表格滚动时),您只添加一个文本字段,然后将其移动到不同的单元格。它最终会出现在最后渲染的任何一个单元格上。

现在的 cellForRow... 方法的方式是,每次出现任何部分的第 0 行和第 1 行 - 包括当用户只是在屏幕上滚动它们时 - 另一个文本视图和标签作为子视图添加到单元格中。上下滚动 100 次,单元格中将有 100 个文本视图和标签堆叠在一起。

为避免这种情况,请仅在创建单元格时添加子视图。所以在这个街区...

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    UILabel *label = /* all of your label init */
    label.tag = kSOME_UNIQUE_INT_CONST;

    UITextField *textField = /* all of your text field init */
    textField.tag = kSOME_OTHER_UNIQUE_INT_CONST

    [cell addSubview:label];
    [cell addSubview:textField];
}

现在,在块之后,您可以假设每个单元格都只有一个标签和一个文本字段。下一个工作是找到它们(它们要么刚刚创建,要么已经在单元格中,如果它被重复使用)......

UILabel *label = (UILabel *)[cell viewWithTag:kSOME_UNIQUE_INT_CONST];
UILabel *textField = (UITextField *)[cell viewWithTag:kSOME_OTHER_UNIQUE_INT_CONST];

现在您已准备好为给定的部分和行配置单元格。这里的关键是要记住你经常得到重复使用的单元格。它们仍将针对已滚动的行进行配置。所以每个“if”块都需要一个对应的“else”块来改变子视图的状态:

if (indexPath.row == 0) {
    label.text = @"Quantity";
    // not sure what view you were adding here, but don't
    // subview adds only in the cell==nil block above
} else {
    label.text = /* whatever the label should be for row != 0 */
    // this is important:  if you change label state in the if, you
    // must specify it's state also in an else, otherwise you'll see
    // leftover state on the label when the cell is reused for a different row
}

【讨论】:

    【解决方案2】:

    你为什么不像你做标签一样初始化文本框?

    你有TextField:

    UITextField *textField;
    

    然后是标签:

    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 35)];
    [lbl1 setFont:[UIFont systemFontOfSize:16]];
    [lbl1 setTextColor:[UIColor blackColor]];
    [lbl1 setBackgroundColor:[UIColor clearColor]];
    

    你为什么不在标签附近试试这个:

    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(LOCATION)];
    [textfield setTextColor:[UIColor blackColor]];
    [textfield setBackgroundColor:[UIColor clearColor]];
    [textfield setBorderStyle:UITextBorderStyleNone];
    

    也可能是您从中获取文本字段的数组中只有两个文本字段?

    也可能是因为您似乎没有实例化一个新文本框,只是一遍又一遍地从实现中添加相同的文本框,示例:

    if (indexPath.row==0) {
    
        lbl1.text = @"Quantity";
        [cell.contentView addSubview:self.qntTF];
    
    }
    
    if (indexPath.row==1) {
        lbl1.text = @"Unit";
        [cell.contentView addSubview:self.unitTF];
    }
    

    【讨论】:

    • 它不工作。我认为将代码放在之前或之后不是问题
    【解决方案3】:

    在返回单元格之前添加[cell setNeedsDisplay];行。

    [单元集NeedsDisplay];
    返回单元格;

    会好的。

    谢谢。

    【讨论】:

    • 它在此重绘表格视图单元格的内容。它在调用时重绘视图的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    相关资源
    最近更新 更多