0

2行n4セクションのuitableviewがあり 、各セルにラベルとテキストフィールドがあります。そして問題は、私のラベルがすべてのセクションにあるのに、私のテキストフィールドが他のセクションで繰り返されていないことです。実際、セルごとに2つのテキストフィールドがあります。1つは通常のテキストフィールドで、もう1つはピッカーテキストフィールドです(TFをクリックするとピッカーがポップアップ表示されます)。1つのセクションでは、両方の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;
4

3 に答える 3

1

現在のコードでは、(表がスクロールするたびに) 思ったよりもはるかに多くのラベルを追加し、テキスト フィールドを 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];
}

ここで、ブロックの後、各セルに正確に 1 つのラベルと 1 つの 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
}
于 2012-07-31T05:33:41.833 に答える
0

ラベルと同じ方法でテキスト ボックスを初期化してみませんか?

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

また、テキストフィールドを取得する配列には、テキストフィールドが2つしかない可能性がありますか?

また、実装から同じものを何度も追加するだけで、新しいテキストボックスをインスタンス化していないように見えるためかもしれません。例:

if (indexPath.row==0) {

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

}

if (indexPath.row==1) {
    lbl1.text = @"Unit";
    [cell.contentView addSubview:self.unitTF];
}
于 2012-07-31T05:09:02.700 に答える
0

セルを返す前に [セル setNeedsDisplay] を追加します。ライン。

[セル setNeedsDisplay];
セルを返します。

大丈夫でしょう。

ありがとう。

于 2012-07-31T05:48:39.027 に答える