アプリ内で使用されるカスタム設定ビューを作成する必要があります。アプリはモックアップであるため、実際には既にモックアップがあり、UITableViewCellStyleValue1 を使用しますが、編集可能なものはありません。基本的に私がしたいのは同じことですが、編集可能なdetailTextLabel(右側のラベル)を使用すると、これを行う最良の方法は何ですか?
5 に答える
これを行う簡単な方法は、detailTextLabelを使用してテキストを表示し、行選択コードのテキスト編集フィールドの表示と非表示を処理することだと思います。私はこれを次のように動作させています:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITextField *tmpView = [self detailLabel:indexPath];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *detailLabel = [cell detailTextLabel];
CGRect tmpFrame = [detailLabel frame]; // the existing frame
tmpFrame.size.width += 3; // space for the cursor
tmpView.frame = tmpFrame;
tmpView.textColor = detailLabel.textColor;
cell.accessoryView = tmpView;
detailLabel.text = nil;
[tableView addSubview:tmpView];
[tmpView becomeFirstResponder];
[tableView setNeedsDisplay];
}
および以下の選択解除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITextField *tmpView = (UITextField *) cell.accessoryView;
UILabel *label = [cell detailTextLabel];
label.text = tmpView.text;
cell.accessoryView = nil;
[tableView setNeedsDisplay];
}
他の唯一のトリックは、cellForRowAtIndexPathの行選択の強調表示をnoneに設定することでした...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITableViewCellStyleValue1 を引き続き使用できます。編集可能にするセルごとに UITextField インスタンスを作成する必要があります。次に、各 UITextField を適切なセルの accessoriesView プロパティに割り当てます。
次の方法で、ラベル テキストの色を一致させることもできます。
yourTextField.textColor = cell.detailTextLabel.textColor;
UITextField インスタンスの frame プロパティをいじって、配置を正しくする必要がある場合があります。
OK、これが私が今持っているコードです。textField は表示されません。理由はわかりません...
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* reuseIdentifier = @"SettingsCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if ( !cell )
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier] autorelease];
[cell setBackgroundColor:[UIColor baeDarkGrayColor]];
UILabel* cellLabel = [cell textLabel];
[cellLabel setTextColor:[UIColor whiteColor]];
[cellLabel setBackgroundColor:[UIColor clearColor]];
[cell setUserInteractionEnabled:YES];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
// Populate cell with corresponding data.
NSDictionary* tableSection = [_tableData objectAtIndex:indexPath.section];
// Set the label
UILabel* textLabel = [cell textLabel];
NSString* labelString = [[tableSection objectForKey:@"itemTitles"] objectAtIndex:indexPath.row];
[textLabel setText:labelString];
// Set the detail string
// UILabel* detailLabel = [cell detailTextLabel];
// NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row];
// [detailLabel setText:detailLabelString];
// Set the accessory view
BAESettingsTableCellAccessoryType accessoryType = [[[tableSection objectForKey:@"itemAccessories"] objectAtIndex:indexPath.row] integerValue];
switch ( accessoryType )
{
case BAESettingsTableCellAccessoryDisclosureIndicator:
{
UIImageView* disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AccessoryDisclosure.png"]];
[cell setAccessoryView:disclosureView];
[disclosureView release];
break;
}
case BAESettingsTableCellAccessoryOnOffSwitch:
{
UISwitch* onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[onOffSwitch setOn:YES];
[cell setAccessoryView:onOffSwitch];
[onOffSwitch release];
break;
}
case BAESettingsTableCellAccessoryNone:
// default:
// break;
{
UITextField* textField = [[UITextField alloc] init];
NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row];
textField.text = detailLabelString;
textField.textColor = [UIColor whiteColor];
[cell setAccessoryView:textField];
[textField release];
break;
}
}
return cell;
}
あはは!はい、分かりました!今私がやろうとしているのは、セルに残っているタイトル ラベルの幅を見て、詳細テキスト フィールドの幅を動的に定義することです。ほとんどの場合、これをシミュレーターで実行すると、タイトル ラベルのサイズは 0 になりますが、ときどき結果が得られます。これは私には意味がありません。おそらく細胞が再利用されているためだと思いますか?何かアイデアはありますか?
textLabel が設定された後に、このコードを追加しました。
CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font];
そして、最初の case ブロックで次のようにします。
CGRect cellFrame = [cell frame];
float detailTextFieldWidth = cellFrame.size.width - ( textLabelSize.width + 50 );
float detailTextFieldHeight = cellFrame.size.height - 1;
NSLog(@"detail text field calculated frame width, height, %f, %f", detailTextFieldWidth, detailTextFieldHeight);
CGRect frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, detailTextFieldWidth, detailTextFieldHeight);
UITextField* textField = [[UITextField alloc] initWithFrame:frame];
テキスト フィールドのフレームを設定したことがないため、テキスト フィールドが表示されません。正の幅と高さでフレームを設定する必要があります。使用する高さは 43 (行の高さ - 1px の灰色の行バッファー) です。