私はテーブルビューに取り組んでおり、セルを動的に作成する初心者です(以前はIBで作成してから、テーブルビューセルコントローラーなどにリンクしていました..)。
すべてがうまく機能し、受け入れられた配列は適切に更新されますが、[self.tableview reloadData] を起動すると、プログラムは古いセルに新しい値を再描画するだけです。たとえば、セル内の uilabel に "TEST CELL" 値がある場合、データを "CELL TEST" に更新して reloadData を起動すると、uilabel は 2 つのラベルが重なり合っているように見え、両方の値が表示されます。(まったく同じ位置と同じサイズの 2 つの uilabel を作成し、それらの値を設定するようなものです)
そして、このイベントは reloadData を起動するたびに発生し、リロードごとに、プログラムは古いものの上に別の uilabel を追加しているように見えます。ここに私のコードがあります:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *lblText1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, 130, 30)];
lblText1.adjustsFontSizeToFitWidth = YES;
lblText1.backgroundColor = [UIColor clearColor];
lblText1.text = [lblText1Array objectAtIndex:indexPath.row];
[cell addSubview:lblText1];
[lblText1 release];
if(indexPath.row<3)
{
UILabel *lblText2 = [[UILabel alloc] initWithFrame:CGRectMake(170, 5, 130, 30)];
lblText2.adjustsFontSizeToFitWidth = YES;
lblText2.backgroundColor = [UIColor clearColor];
lblText2.text = nil;
lblText2.text = [spParameters objectAtIndex:indexPath.row];
[cell addSubview:lblText2];
[lblText2 release];
}
else if(indexPath.row==3){
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(170, 7, 130, 30)];
textField.adjustsFontSizeToFitWidth = YES;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = @"please insert value";
textField.textAlignment = UITextAlignmentCenter;
textField.delegate = self;
textField.text = [spParameters objectAtIndex:indexPath.row];
[cell addSubview:textField];
[textField release];
}
else if(indexPath.row == 4)
{
UISwitch *gSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(170, 7, 130, 30)];
[gSwitch setOn:FALSE];
[gSwitch addTarget: self action: @selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:gSwitch];
[gSwitch release];
}
// Configure the cell...
return cell;
}
コンポーネントをサブビューに追加した後にコンポーネントを解放していますが、reuseidentifier に何か問題があるかどうかを考えています...
助けてくれてありがとう。