2012-07-31 80 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