2012-07-31 49 views
0

我有一个可用的视图2行n 4个部分,我在每个单元中有一个标签和文本字段。问题是我的标签出现在所有部分,因为我的文本字段在其他部分不重复。实际上,每个单元格都有两个textfield单元格,一个是普通文本框,另一个是拾取器文本框(当我单击TF时,拾取器会弹出)。 对于一个部分TF都会出现,但不会在其他部分中重复。在uitableview问题中的两个Textfield问题

我的代码

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; 
+0

嗨 - 请再看看我的答案。那里的模式将解决你的问题。你添加了太多的标签(每次单元格被绘制时都会添加一个),并且你还一遍又一遍地添加相同的文本字段。因为它是一样的,它只是从一个单元移动到另一个单元。你只会在被调用最后渲染的单元上看到它。 – danh 2012-07-31 14:06:28

+0

@danh我试过了,现在问题是什么问题,它没有得到所有的单元格被称为只有两个部分,1保留空标签,而不是texfield。我的选择器现在创建问题 – 2012-08-01 05:37:27

回答

0

你为什么不初始化文本框你做的标签以同样的方式?

你的文本字段:

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]; 
} 
+0

它不工作。我认为在 – 2012-07-31 05:23:54

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]; 
} 

现在,在块之后,你可以假定每个单元格都有一个标签和一个textField。接下来的工作就是找到它们(他们要么刚建立,或者已经在细胞如果被重复使用)...

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

现在,您可以将单元配置为给定的部分和行。这里的关键是要记住,你经常获得重用的单元格。它们仍将配置为已滚动的行。所以,每一个“如果”块需要一个相应的“其他人”块更改子视图中指出:

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 
} 
0

返回之前的细胞中添加[细胞setNeedsDisplay];线。

[cell setNeedsDisplay];
return cell;

它会好的。

thanx。

+0

之前或之后的代码不起作用并不是问题。它没有任何影响 – 2012-07-31 06:11:10

+0

它在这里重绘表格视图单元格的内容。 它调用时重绘视图的内容。 – damithH 2012-08-15 04:20:45